The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
What type of operand is an Array Index?
Discuss What type of operand is an Array Index? in the PHP Development forum on Dev Shed. What type of operand is an Array Index? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 19th, 2013, 11:25 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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
In what way or how can the index be an array? I will appreciate an example.
Thanks!
|

February 19th, 2013, 11:32 PM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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.
|

February 19th, 2013, 11:52 PM
|
 |
Still alive
|
|
Join Date: Mar 2007
Location: Washington, USA
|
|
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
is invalid.
|

February 20th, 2013, 12:02 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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
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.
|

February 20th, 2013, 01:16 AM
|
 |
Lost in code
|
|
|
|
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).
|

February 20th, 2013, 05:11 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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.
|

February 20th, 2013, 05:22 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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' => 4, 5, 'a' => 10, 2);
The values 4 and 10 are indexed by 'x' and 'a' respectively. However, 5 and 2 are indexed automatically with 0 and 1 respectively.
|

February 20th, 2013, 05:29 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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!
|

February 20th, 2013, 05:44 AM
|
 |
Square Peg in a Round Hole
|
|
Join Date: Oct 2007
Location: North Yorkshire, UK
|
|
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
)
)
)
)
)
|

February 20th, 2013, 06:03 AM
|
|
Registered User
|
|
Join Date: Feb 2013
Posts: 6
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|