danludwig Posted August 6, 2009 Share Posted August 6, 2009 Hi, I don't have a PHP execution environment to test in, and am trying to read some PHP code to determine what it does. Say I declare a variable like so: $var = array(); If I remember correctly, I can put values into this array like so: $var["key1"] = "Some value"; $var[2] = "Some other value"; On the other hand, what does it mean if I do this? $var[] = "Some third value"; Does this stack the value onto the end of the array? If so, does it not have a key, or does it use NULL as a key? Also, in more strongly-typed languages, I'm used to managing keyed collections like this in objects like Hashtable (Java / CSharp / VB) rather than arrays. Hashtable in particular usually allows the use of NULL as a key once. Is it the same way in PHP? For example, if I have: $key = null; $array = array(); $array[$key] = "This is important information"; ...how would I access the important information? By calling $array[null] on the right side of an expression? Thanks, Dan Quote Link to comment https://forums.phpfreaks.com/topic/169148-solved-square-brackets-after-variable-what-does-this-do/ Share on other sites More sharing options...
taquitosensei Posted August 6, 2009 Share Posted August 6, 2009 It adds to the array with the index being the next numerical value. If you have this $array[1]="blah"; this $array[2]="whatever"; and $array[]="whatever"; are the same thing I don't think php allows null keys. Numerically indexed arrays are generally accessed in a loop. If you need to access by a specific index. You would use an associative array. $array['myname']="Mr. Coffee"; Quote Link to comment https://forums.phpfreaks.com/topic/169148-solved-square-brackets-after-variable-what-does-this-do/#findComment-892456 Share on other sites More sharing options...
mikesta707 Posted August 6, 2009 Share Posted August 6, 2009 hmm, never tried to use a key as a null value before... but yes $var[] will stack the value onto the end of the array. For numeric arrays, the key will just be the largest key +1. For an associative array, if you have no numbers as keys, the key will be zero. if you have a number, it will be the next increment. for example, the following code: <?php $array = array("hi" => "too", "toad" => "pond", "goop" => "test", 0 => "ddd"); $array[] = "dd"; foreach($array as $key => $value){ echo $key . " => " . $value . "<br />"; } ?> would output: hi => too toad => pond goop => test 0 => ddd 1 => dd (which I did verify on my test server) and yes, you can use null as a key and call it via $array[null] (which I tested also) hope that helps! Quote Link to comment https://forums.phpfreaks.com/topic/169148-solved-square-brackets-after-variable-what-does-this-do/#findComment-892457 Share on other sites More sharing options...
ldougherty Posted August 6, 2009 Share Posted August 6, 2009 If you do not supply an index it will just add the item to the end of the array. var[0] = 1; var[1] = 2; var[] = 3; outputting the array would yield 1,2,3 Quote Link to comment https://forums.phpfreaks.com/topic/169148-solved-square-brackets-after-variable-what-does-this-do/#findComment-892458 Share on other sites More sharing options...
Maq Posted August 6, 2009 Share Posted August 6, 2009 To elaborate and clarify on ldougherty, the print_r() display would look like: Array ( [0] => 1 [1] => 2 [2] => 3 ) (You also need to prefix the array with '$') Quote Link to comment https://forums.phpfreaks.com/topic/169148-solved-square-brackets-after-variable-what-does-this-do/#findComment-892482 Share on other sites More sharing options...
danludwig Posted August 6, 2009 Author Share Posted August 6, 2009 Thanks everyone for the quick replies. I was confused because the code I'm looking at does in fact initialize a variable $key = null; then soon afterward assigns a value using $someArray[$key] = $someOtherVariable. I vaguely remembered from my PHP days that the brackets were a shortcut for adding to the end of an array, but wanted to make sure that this wasn't a shortcut to get the value stored under the NULL key. To confirm, if I create an array like so: $myArray = array(); $myArray["key1"] = "value1"; $myArray[] = "value2"; $myArray[1] = "value3"; $myArray[] = "value4"; ... then the corresponding keys of the array, in order, are "key1", 0, 1, 2. Right? I suppose my trick is going to be trying to figure out how to mimic a data structure like this in a more strongly-typed language. I was thinking Hashtable would work, but to add a value to one, you always have to specify a key. Guess I can just write a little helper method to auto-assign next int values and mimic an associative array. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/169148-solved-square-brackets-after-variable-what-does-this-do/#findComment-892487 Share on other sites More sharing options...
mikesta707 Posted August 6, 2009 Share Posted August 6, 2009 Thanks everyone for the quick replies. I was confused because the code I'm looking at does in fact initialize a variable $key = null; then soon afterward assigns a value using $someArray[$key] = $someOtherVariable. I vaguely remembered from my PHP days that the brackets were a shortcut for adding to the end of an array, but wanted to make sure that this wasn't a shortcut to get the value stored under the NULL key. To confirm, if I create an array like so: $myArray = array(); $myArray["key1"] = "value1"; $myArray[] = "value2"; $myArray[1] = "value3"; $myArray[] = "value4"; ... then the corresponding keys of the array, in order, are "key1", 0, 1, 2. Right? I suppose my trick is going to be trying to figure out how to mimic a data structure like this in a more strongly-typed language. I was thinking Hashtable would work, but to add a value to one, you always have to specify a key. Guess I can just write a little helper method to auto-assign next int values and mimic an associative array. Thanks again. Indeed they are. Just to test I ran the following script (basically yours with a foreach to output) <?php $myArray = array(); $myArray["key1"] = "value1"; $myArray[] = "value2"; $myArray[1] = "value3"; $myArray[] = "value4"; foreach($myArray as $key => $value){ echo $key . " => " . $value . "<br />"; } ?> and the output was as follows: key1 => value1 0 => value2 1 => value3 2 => value4 so again, yes you are correct Quote Link to comment https://forums.phpfreaks.com/topic/169148-solved-square-brackets-after-variable-what-does-this-do/#findComment-892492 Share on other sites More sharing options...
danludwig Posted August 6, 2009 Author Share Posted August 6, 2009 Awesome, question solved/answered. Thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/169148-solved-square-brackets-after-variable-what-does-this-do/#findComment-892493 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.