This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
4.1.2 Associative Arrays
- Associative arrays are arrays that use named keys that you assign to them.
- There are two ways to create an associative array:
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
or
$age['Peter'] = "35";
$age['Ben'] = "37";
$age['Joe'] = "43";
The named keys can then be used in a script:
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>
Associative arrays are arrays that use named keys that you assign to them.
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
var_dump($car);
Access Associative Arrays
To access an array item you can refer to the key name.
Display the model of the car:
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
echo $car["model"];
Change Value
To change the value of an array item, use the key name:
Change the year item:
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
$car["year"] = 2024;
var_dump($car);
Loop Through an Associative Array
To loop through and print all the values of an associative array, you could use a foreach loop, like this:
Display all array items, keys and values:
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
foreach ($car as $x => $y) {
echo "$x: $y <br>";
}
Another example
<?php
$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
foreach($age as $x => $x_value) {
echo "Key=" . $x . ", Value=" . $x_value;
echo "<br>";
}
?>
Complete PHP Array Reference
Animated PHP icons used in the buttons provided by ICONS8.COM.
Smartphone icons created by Freepik - Flaticon
Module 4 quiz
Example files created in this module:
PHP Arrays
Array Items
PHP Arrays - Get The Length of an Array with The count() Function
PHP Arrays - Change Value
Indexed Arrays - Loop Through an Indexed Array
PHP Arrays: Index Number part 1
PHP Arrays: Index Number part 2
PHP Arrays - Associative Arrays part 1
PHP Arrays - Associative Arrays part 2
PHP Arrays - Access Associative Arrays
PHP Associative Arrays: Change Key's Value
Associative Arrays - Loop Through an Associative Array part 1
Associative Arrays - Loop Through an Associative Array part 2
PHP Arrays - Two-dimensional Arrays
PHP Arrays - Two-dimensional Arrays with for loop
PHP Arrays - Sorting Arrays part 1
PHP Arrays - Sorting Arrays part 2
PHP Arrays - Sort Array in Descending Order rsort() part 1
PHP Arrays - Sort Array in Descending Order rsort() part 2
PHP Arrays - Sort Array (Ascending Order), According to Value asort()
PHP Arrays - Sort Array (Ascending Order), According to Key ksort()
PHP Arrays - Sort Array (Descending Order), According to Value arsort()
PHP Arrays - Sort Array (Descending Order), According to Key krsort()
PHP $GLOBALS
PHP $_SERVER
PHP $_REQUEST - Form Submission
PHP $_POST - Form Submission
Test Get
PHP RegEx - Using preg_match()
PHP RegEx - Using preg_match_all()
PHP RegEx - Using preg_replace()
PHP RegEx - Grouping