Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Array - brackets

Tags:

arrays

php

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);
like image 306
Krooy_mans Avatar asked Mar 28 '15 07:03

Krooy_mans


Video Answer


1 Answers

Read http://php.net/manual/en/function.array-push.php for a clear answer as to the differences.

main differences:

  • citing array() can only be done when creating the variable.
  • $var[] can add values to arrays that already exist.
  • $var[] will create an array and is in effect the same function call as $var = array(...);

also

  • You can not assign multiple values in a single declaration using the $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
like image 57
Martin Avatar answered Sep 24 '22 18:09

Martin