PHP gotcha of the day: tricky arrays

Indexes, keys, values ... they all have some surprises for you

Published on October 05, '11
by Serban Ghita
3
Comment
ShareThis

As a PHP programmer you are dealing with arrays every single day, meaning that there is a high probability of hitting some "unexpected" results. In this article we will not deal with array operations and functions.

Try to answer the following questions, we'll explain the results at the end of this article:

<?php
// a.) What is the output of count($array)?
$array = array(=> "Serban""1" => "Ghita""John"=> "Doe");
?>
Spoiler: Highlight to view
2
<?php
// b.) What will the following script display? (1 answer)
$array = array('Serban' => 'Ghita''John' => 'Smith');
function 
myName(){
  return 
'Serban';
}
echo 
$array[myName()];
// 1. Parse error
// 2. Ghita
// 3. NULL
// 4. Notice: Use of undefined constant myName + NULL
?>
Spoiler: Highlight to view
Ghita
<?php
// c.) What is the output of strlen($text) ?
// d.) What is the output of count($text) ?
$text 'Serban';
$text[7] = 'Ghita';
?>
Spoiler: Highlight to view
8 1 (ok I know this is not an array question, I'm playing with your mind)
<?php
// e.) What is the resulting key of the value 'Serban' ?
$array = array(=> 'Octav'=> 'Bogdan'=> 'Marius'=> 'Ciprian'=> 'Iulian');

foreach(
$array as $key => $value){
  unset(
$array[$key]);
}

$array[] = 'Serban';
?>
Spoiler: Highlight to view
5
<?php
// f.) What is the result of the var_dump() function?
$a = array(false => 'Serban'true => 'Ghita');
$b = array('Serban''Ghita');

var_dump($a===$b);
?>
Spoiler: Highlight to view
true
<?php
// g.) What is the output?
$a = array(123);
$b = &$a[0];
$a2 $a;
$a2[0]++;

echo( 
$a[0] );
?>
Spoiler: Highlight to view
2

Answers explained

a.) Array keys can only be of integer and string type. '1' will be interpreted as 1, but '01' will be interpreted as '01'. If you provide an array with two identical keys the latter will override the former. If a key is not specified for a value, the maximum of the integer indices is taken and the new key will be that value plus 1.

b.) In an array the key between the square brackets '[' ']' can be an expression, that means both $arr[test()] and $arr[test($var)] is valid code.

c.) and d.) 'Serban' string has a length of 6. $text[7] adds 'G' character on the 8th position and automatically pads the 7th position with a space, resulting 'Serban G'.
Obviously count($string) that is not null is always 1. Note that count(null) is 0.

e.) Unsetting keys in $array doesn't resets the index value. Doing reset($array) will reset the index, doing unset($array) will destroy the array and reset the index.

f.) Remember that array keys can only be strings or integers, so false evaluates to 0 and true to 1. The arrays are identical and the keys are in the same order.

Note that:

<?php
$a 
= array(=> 'Serban'=> 'Ghita');
$b = array(=> 'Ghita'=> 'Serban');

var_dump($a==$b); // bool(true)
var_dump($a===$b); // bool(false)
?>

g.) When arrays are copied, the "reference status" of their members is preserved.

Comments

Awais Qarni
Written on: 08 February 2012
Boss this is totally awesome. you have once shacked my all concepts. But your blog has insisted me to work more hard. Thanks for sharing
shakil
Written on: 23 March 2012
Guru really awesome all think. thanks for sharing. i am happy for visit your blog.
Guest
Written on: 01 February 2013
Hi, For the last Question , the answer for the output is 1.
<?php
$a 
= array(1,2,3);
        
$b = &$a[0];
        
        
$a2 $a;
        
$a2[o]++;
        
        echo
"b is"
        print(
$b);  // result is 1
        
echo "<br/>";
        echo 
"a[0] is ";
        echo 
$a[0];// result is 1
?>
Thanks

Post new comment

This blog is intended to foster polite on-topic discussions. Comments failing these requirements and spam will not get published.