PHPAssociative Arrays
PHP Associative Arrays
Associative arrays use named keys, instead of numeric indices.
Example
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);var_dump($car);Try it Yourself »Access Array Item
To access a specific array item, refer to the key name.
Example
Display the model of the car:
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);echo $car["model"];Try it Yourself »Change Value of Array Item
To change the value of an array item, use the key name:
Example
Change theyear item:
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);$car["year"] = 2024;var_dump($car);Try it Yourself »Loop Through an Associative Array
To loop through and print all the values of an associative array, use aforeach loop, like this:
Example
Display all array items, keys and values:
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);foreach ($car as $x => $y) { echo "$x: $y <br>";}Try it Yourself »For a complete reference of all array functions, go to our completePHP Array Reference.

