PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 19th, 2013, 11:25 PM
devphphreak devphphreak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 devphphreak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 28 sec
Reputation Power: 0
What type of operand is an Array Index?

Hi there,

I got some time to revise programming (using a PHP book) and have come across a fundamental and probably simpler than I think question which answer I want to understand. I appreciate all feedback.

The question is "What type of operand is an Array Index?".

From what I know of an array, its index is the reference to the elements in the array. The book tells me the type of operand can be an integer, string or array. I know how the index can be an integer and string.

For example,
PHP Code:
 $alphabet = array('a'=>'aay''b'=>'bee''c'=>'see'4=>'four'
is a case in which the index 'a' is a string and can also be referenced using the number 0
PHP Code:
 $alphabet[0


In what way or how can the index be an array? I will appreciate an example.

Thanks!

Reply With Quote
  #2  
Old February 19th, 2013, 11:32 PM
devphphreak devphphreak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 devphphreak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 28 sec
Reputation Power: 0
Right after posting, I tried an example (by replacing one of the indices of the array with an array) that I thought might work, and it worked. So I realised how an Array Index can be of type array. I think I fully understand this now.

Reply With Quote
  #3  
Old February 19th, 2013, 11:52 PM
requinix's Avatar
requinix requinix is offline
Still alive
Click here for more information.
 
Join Date: Mar 2007
Location: Washington, USA
Posts: 12,676 requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)requinix User rank is General 120th Grade (Above 100000 Reputation Level)  Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1Folding Points: 417516 Folding Title: Super Ultimate Folder - Level 1
Time spent in forums: 5 Months 1 Week 4 Days 1 h 40 m 59 sec
Reputation Power: 8969
Send a message via AIM to requinix Send a message via MSN to requinix Send a message via Yahoo to requinix Send a message via Google Talk to requinix
Quote:
Originally Posted by devphphreak
Right after posting, I tried an example (by replacing one of the indices of the array with an array) that I thought might work, and it worked. So I realised how an Array Index can be of type array. I think I fully understand this now.

No, it can't. I don't know what you tried but an array key cannot be an array. So
PHP Code:
array(array() => 123

is invalid.

Reply With Quote
  #4  
Old February 20th, 2013, 12:02 AM
devphphreak devphphreak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 devphphreak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 28 sec
Reputation Power: 0
I tried
PHP Code:
 $alphabet = array('array(a)'=>'aay''b'=>'bee''c'=>'see'4=>'four'); 


I am just realising my error. I made 'array(a)' a string instead of leaving it as an array. And when I tried using array(a) without the quotes, so it is a proper array, I got NULL after referencing it like so
PHP Code:
 $alphabet[array('a')] 


Which means this book is lying to me! And getting me 'foncused' about this. Arrgh. Or might it be that I do not understand the question? If I do understand the question, then it means an Array Index can only be a number or a string. Right?

Thank you requinix for looking at this closely for me.

Reply With Quote
  #5  
Old February 20th, 2013, 01:16 AM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 43 m 47 sec
Reputation Power: 6991
PHP Code:
 $alphabet = array('a'=>'aay''b'=>'bee''c'=>'see'4=>'four'

Quote:
is a case in which the index 'a' is a string and can also be referenced using the number 0

In that example,
$alphabet['a'] will be 'aay'.
$alphabet[0] is undefined, it does not reference 'aay'.

An array index can be a string, a number, true/false or null. It cannot be an array or object.

Array indexes are rarely referred to as operands (in any programming language, not just PHP), and doing so is sort stretching the meaning of the word.

If the book's answer is a string or number then I can understand the question. However, I cannot think of any context involving array indexes in which an array, a string and a number can syntactically be used interchangeable (except as the right-hand operand in an assignment statement).
__________________
PHP FAQ
How to program a basic, secure login system using PHP

Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #6  
Old February 20th, 2013, 05:11 AM
devphphreak devphphreak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 devphphreak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 28 sec
Reputation Power: 0
Thanks E-Oreo! I found your post very insightful. And thank you for the correction as well. It shows me in an associative array, the indices specified are exactly that and their values cannot be referenced any other way except through the indices themselves.

Reply With Quote
  #7  
Old February 20th, 2013, 05:22 AM
Jacques1's Avatar
Jacques1 Jacques1 is offline
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,834 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 2 h 1 m 49 sec
Reputation Power: 811
Well, unfortunately, it's not quite that simply. The PHP developers had the genius idea of intermixing the concept of arrays (which are numerically indexed according to the order of the elements) and hashes (which have custom indices). This means you can have weird hybrids like this:
PHP Code:
 $a = array('x' => 45'a' => 102); 

The values 4 and 10 are indexed by 'x' and 'a' respectively. However, 5 and 2 are indexed automatically with 0 and 1 respectively.

Reply With Quote
  #8  
Old February 20th, 2013, 05:29 AM
devphphreak devphphreak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 devphphreak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 28 sec
Reputation Power: 0
Ah. Even more insightful! Weird way to intermix indexing, if you asked me. Thanks Jacques1!

Reply With Quote
  #9  
Old February 20th, 2013, 05:44 AM
Northie's Avatar
Northie Northie is offline
Square Peg in a Round Hole
Click here for more information.
 
Join Date: Oct 2007
Location: North Yorkshire, UK
Posts: 3,415 Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level)Northie User rank is General 43rd Grade (Above 100000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 9 h 21 m 40 sec
Reputation Power: 3833
array values can be arrays.

Have fun!

PHP Code:
 $a = array(
    array(
        
'a',
        
'b',
        
'c' => 'C'
    
),
    
'foo'=>array(1,2,3),
    array(
        
'a','b','c'
    
),
    
8,
    
9,
    
10 => array(),
    
'bar'=>'baz',
    
'qux'=>array(
        array(
            array(
                
'x'=>'y',
                
'z',
                
9,
                
8,
                
7
            
)
        )
    )
);

$a['test'] = $a;

print_r($a); 


Code:
Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [c] => C
        )

    [foo] => Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )

    [1] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

    [2] => 8
    [3] => 9
    [10] => Array
        (
        )

    [bar] => baz
    [qux] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [x] => y
                            [0] => z
                            [1] => 9
                            [2] => 8
                            [3] => 7
                        )

                )

        )

    [test] => Array
        (
            [0] => Array
                (
                    [0] => a
                    [1] => b
                    [c] => C
                )

            [foo] => Array
                (
                    [0] => 1
                    [1] => 2
                    [2] => 3
                )

            [1] => Array
                (
                    [0] => a
                    [1] => b
                    [2] => c
                )

            [2] => 8
            [3] => 9
            [10] => Array
                (
                )

            [bar] => baz
            [qux] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [x] => y
                                    [0] => z
                                    [1] => 9
                                    [2] => 8
                                    [3] => 7
                                )

                        )

                )

        )

)
__________________
PHP OOPS! <?php DB::Execute(SQL::makeFrom($_GET))->fetchArray()->FormatWith(Template::getInstance('default'))->printHtml(); ?>

PDO vs mysql_* functions: Find a Migration Guide Here

[ Xeneco - T'interweb Development ] - [ Are you a Help Vampire? ] - [ Read The manual! ] - [ W3 methods - GET, POST, etc ] - [ Web Design Hell ]

Reply With Quote
  #10  
Old February 20th, 2013, 06:03 AM
devphphreak devphphreak is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 6 devphphreak User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 2 m 28 sec
Reputation Power: 0
Thank you Northie. I think you have successfully changed the direction of the conversation. However, the information you provide is a useful reminder.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > What type of operand is an Array Index?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap