Tag Archive: array


When to use []

This is a shortcut for array_push. It’s easier and even better if you add one element to the array.

Note: If you use array_push() to add one element to the array it's better to use $array[] = because in that way there is no overhead of calling a function.

If you make a mistake and you try to use this on something else than an array, you will have a warning both case. In the doc, you have something else

Note: array_push() will raise a warning if the first argument is not an array. This differs from the $var[] behaviour where a new array is created.
$arr = 5;
array_push($arr, 4);

// PHP Warning:  array_push() expects parameter 1 to be array, integer given

$arr = 5;
$arr[] = 4;

// PHP Warning:  Cannot use a scalar value as an array

// PHP 5.6.7-1+deb.sury.org~trusty+1 (cli) (built: Mar 24 2015 11:21:10) 
// Copyright (c) 1997-2015 The PHP Group
// Zend Engine v2.6.0, Copyright (c) 1998-2015 Zend Technologies
//     with Zend OPcache v7.0.4-dev, Copyright (c) 1999-2015, by Zend Technologies
//     with Xdebug v2.3.1, Copyright (c) 2002-2015, by Derick Rethans

When not to use []

This is a list of when not to use [] and will result in this error

$a = array();
$b = $a[];
///////////////////////////////////
$a = array();
isset($a[]);
///////////////////////////////////
function test($var) { }
test($arr[]);
///////////////////////////////////
$a = array();	
foreach($a[] as $b) {
}
///////////////////////////////////
$a = array();	
foreach($a[]['test'] as $b) {
}
///////////////////////////////////
$a = array();
foreach($a['test'][] as $b) {
}
///////////////////////////////////

You can find more example here

The problem i had

I came accros some code trying to use variables variables and []. Since the precedence is kind of blurry here, i got this error in this particular scenario.

$test = 'var';
$name = 'test' . $test;

$testvar = array();
$$name[] = 3; // Add something to $testvar

// PHP Fatal error:  Cannot use [] for reading

The solution

1- Use array_push : array_push($$name, 3);
2- Use {} to force precedence : ${$name}[] = 3;

What i have found

I ran it over 3v4l and it seems to work now on php7@20140901 – 20150501

Advertisements

Tips and Tricks #1

FOREACH reference:

<?php 
$myArray = array('t','h','i','s','i','s','a','n','a','r','r','a','y');
foreach($myArray as &$value) {
  echo $value;
}
echo PHP_EOL;
foreach($myArray as $v) {
  echo $v;
}
echo PHP_EOL;
foreach($myArray as $value) {
  echo $value;
}
/*
OUTPUT:
thisisanarray
thisisanarray
thisisanarraa
*/

The reason is almost obvious when you know you’re using reference and you know how reference works. When you are using reference , $value is a pointer to your element , not a copied value. So when you loop over your array, $value will be each element from your array. Everything is ok until you use the variable again. Everything you will assign to it , will also change the value of the reference. Another example to explain :

<?php   
$myArray = array('t','h','i','s');  
 foreach($myArray as &$value) {
           echo $value;
   }   
  echo PHP_EOL;
  $value = 'reference value changed';
  var_dump($myArray);
/*  OUTPUT:  
this 
array(4) {
  [0]= 
  string(1) "t"
  [1]=---->
  string(1) "h"
  [2]=>
  string(1) "i"
  [3]=>
  &string(23) "reference value changed"
}
*/

You only need to unset the reference. In this case : $value. If you want a more detail explication , check out Johannes Schlüter blog