does someone know the meaning of the [ ] in the creation of a PHP array, and if it's really needed. Becasuse from my point of view. Both ways are sufficinent
Way 1, with brackets:
$cars[] = array ('expensive' => $BMW,
'medium' => $Volvo,
'cheap' => $Lada);
Way2, without brackets:
$cars = array ('expensive' => $BMW,
'medium' => $Volvo,
'cheap' => $Lada);
Read http://php.net/manual/en/function.array-push.php for a clear answer as to the differences.
main differences:
array()
can only be done when creating the variable.$var = array(...);
also
$var[]
approach (see example 4, below).some work throughs:
1) Using array()
$cars = "cats";
$cars = array("cars","horses","trees");
print_r($cars) ;
Will output
$cars --> [0] = cars
[1] = horses
[2] = trees
2) Appending Values
Then writing $cars = array("goats");
will NOT append the value but will instead initialise a NEW ARRAY, giving:
$cars --> [0] = goats
But if you use the square brackets to append then writing $cars[] = "goats"
will give you :
$cars --> [0] = cars
[1] = horses
[2] = trees
[3] = goats
Your original question means that whatever is on the right hand side of the =
sign will be appended to current array, if the left hand side has the syntax $var[]
but this will be appended Numerically. As shown above.
You can append things by key name by filling in the key value: $cars['cheap'] = $Lada;
.
Your example 1 is setting that an array is held within an array, so to access the value $Lada you would reference $cars[0]['cheap']
. Example 2 sets up the array but will overwrite any existing array or value in the variable.
3) String And Numerical Indexing
The method of using the array(...)
syntax is good for defining multiple values at array creation when these values have non-numeric or numerically non-linear keys, such as your example:
$cars = array ('expensive' => "BMW",
'medium' => "Volvo",
'cheap' => "Lada");
Will output at array of:
$cars --> ['expensive'] = BMW
['medium'] = Volvo
['cheap'] = Lada
But if you used the alternative syntax of:
$cars[] = "BMW";
$cars[] = "Volvo";
$cars[] = "Lada";
This would output:
$cars --> [0] = BMW
[1] = Volvo
[2] = Lada
4) I'm still writing....
As another example: You can combine the effects of using array()
and $var[]
with key declarations within the square brackets thus:
$cars['expensive'] = "BMW";
$cars['medium'] = "Volvo";
$cars['budget'] = "Lada";
giving:
$cars --> ['expensive'] = BMW
['medium'] = Volvo
['cheap'] = Lada
(my original answer was verbose and not very great).
5) Finally.....
So what happens when you combine the two styles, mixing the array()
declaration with the $var[]
additions:
$ray = array("goats" => "horny", "knickers" => "on the floor", "condition" => "sour cream");
$ray[] = "crumpet";
$ray[] = "bread";
This will maintain both numeric and string key indexes in the array, outputting with print_r()
:
$ray --> [goats] => horny
[knickers] => on the floor
[condition] => sour cream
[0] => crumpet
[1] => bread
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With