Jump to content

[SOLVED] Square Brackets after variable -- what does this do?


Recommended Posts

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

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";

 

 

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!

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.

 

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.