Hudzilla.org - the homepage of Paul Hudson
Contents > Arrays Wish List | Report Bug | About Me ]

5.2     Associative arrays

This is NOT the latest copy of this book; click here for the latest version.

As well as choosing individual values, you can also choose your keys - in the fruits code above we just specify values, but we could have specified keys along with them, like this:

<?php
    $myarray
= array("a"=>"Apples", "b"=>"Oranges", "c"=>"Pears");
    
var_dump($myarray);
?>

This time, var_dump() will output the following:

array(3) {
    ["a"]=>
    string(6) "Apples"
    ["b"]=>
    string(7) "Oranges"
    ["c"]=>
    string(5) "Pears"
}

As expected, our 0, 1, and 2 element keys have been replaced with a, b, and c, but we could equally have used "Foo", "Bar", and "Baz", or even variables or other arrays to act as the keys. Specifying your own keys produces what is called an associative array - you associate a specific key with a specific value. Most associative arrays use strings as keys, but do not be afraid to try more advanced things out.

The one exception here is floating-point numbers: these make very poor array indexes. The problem lies in the fact that PHP converts them to integers before they are used, which essentially rounds them down. So, the following code will create an array with just one element in:

<?php
    $array
[1.5] = "foo";
    
$array[1.6] = "bar";
?>

The first line will read 1.5 as the key, round it down to 1, then store "foo" at index 1. The second line will read 1.6 as the key, also round it down to 1, then store "bar" at index 1, overwriting "foo". If you really want to use floating-point numbers as your keys, pass them in as strings, like this:

<?php
    $array
["1.5"] = "foo";
    
$array["1.6"] = "foo";
    
var_dump($array);
?>

That should output the following:

array(2) {
    ["1.5"]=>
    string(3) "foo"
    ["1.6"]=>
    string(3) "foo"
}

This time the floating-point numbers have not been rounded down or converted at all, because PHP is using them as strings. The same solution applies to reading values out from an associative array with floating-point keys - you must always specify the key as a string.





<< 5.1 First steps: array(), count(), print_r(), var_dump(), and var_export()   5.3 The three ways of iterating through arrays: list(), each(), and foreach loops >>
Table of Contents
Want to see this stuff in print? PHP in a Nutshell takes the core topics covered here, adds in thousands of edits from the editorial team and myself, and combines them to make an unbeatable reference for PHP programmers at all levels.



My latest book has hundreds more tips on how to use PHP, Apache, and MySQL, plus Perl, Python, shell scripts, performance tuning, and more!



Top-right shadow
 
Bottom-left shadow Bottom shadow

Comments from other readers
Moony - 06 Sep 2008

Thanks a lot

Shafiq - 06 Sep 2008

THIS IS REALLY HELPFUL NOTE FOR ME

A PHP User - 06 Sep 2008

Why should i use this feature? Case statement?



Add comment
Please note that by posting a comment here you are committing it to the public domain. This is important so that others can make use of your code themselves, and also so that I can incorporate helpful notes directly into the main text. Comments are limited to 2000 characters in length.

If you are reporting an error in the content, please tell me directly.

Your name/email address:
Your comment:
 
Now, in order to verify that you're a real person, please answer this simple question: what is six plus eight?
The answer is:
(please write in
numbers, eg 19)


Top-right shadow
 
Bottom-left shadow Bottom shadow