In this lesson, you will learn about the Associative Array in PHP, its usage, and examples to better understand the topic.
Associative arrays use descriptive Id key names, which is how they differ from numeric arrays. When obtaining data from the database, associative arrays are also excellent choices.
1st Method:
$nameoffruits=array("key1"=>”value1”," key2"=>”value2”," key3"=>”value3”); // generic $nameoffruits=array(" fruits1" => "Apple", " fruits2" => "Banana", " fruits3" => "Orange" );
2nd Method:
$ nameoffruits [‘fruits1’]=" apple"; $ nameoffruits [‘fruits2’]=" banana"; $ nameoffruits [‘fruits3’]=" orange"; $ nameoffruits [‘fruits4’]=" pineapple"; “$nameoffruits” is the name of the variable “[key1]” is the access index number of the member "value" is the value allocated to the array member.
An integer or a string can be the key, and any value can be the value.
The following key casts will also occur:
+
sign precedes the number. E.g., the “8” key will be stored below 8. On the other hand, as it is not a valid decimal integer, “08” will not be cast.Let’s see an example to understand typecasting. Since all the keys in the below instance are cast to 1, the value of each fresh element will be overwritten, and the last allocated value, “d,” is the only one left over.
<?php echo "PHP ***Associative Array*** Example"; echo "<br>"; echo " ------------Type casting ------------------ "; echo "<br>"; $array = array( 1 => "value1", "1" => "value2", 1.5 => "value3", true => "value4", ); var_dump($array); ?>
Output
PHP ***Associative Array*** Example ------------Type casting ------------------ array(1) { [1]=> string(6) "value4" }
At the same time, as PHP does not differentiate between indexed and associative arrays, PHP arrays may contain integer and string keys.
<?php echo "PHP ***Associative Array*** Example"; echo "<br>"; echo " ------------String and Integer Array (Mixed) ------------------ "; echo "<br>"; $array = array( "foo" => "bar", "bar" => "foo", 22 => - 22, -22 => 22, ); var_dump($array); ?>
Output
PHP ***Associative Array*** Example ------------String and Integer Array (Mixed) ------------------ array(4) { ["foo"]=> string(3) "bar" ["bar"]=> string(3) "foo" [22]=> int(-22) [-22]=> int(22) }
Optional is the key. If it is not indicated, the biggest earlier used significant integer increment will be used by PHP.
<?php echo "PHP ***Associative Array*** Example"; echo "<br>"; echo " ------------String and Integer Array (Mixed) ------------------ "; echo "<br>"; $array = array( "DevelopedCountryName1" => "Canada", "DevelopedCountryName2" => "South Africa", 22 => - 22, -22 => 22, ); var_dump($array); ?>
Output
PHP ***Associative Array*** Example ------------String and Integer Array (Mixed) ------------------ array(4) { ["DevelopedCountryName1"]=> string(6) "Canada" ["DevelopedCountryName2"]=> string(12) "South Africa" [22]=> int(-22) [-22]=> int(22) }
Only for some components can the key be specified and left out for others.
<?php echo "PHP ***Associative Array*** Example"; echo "<br>"; echo " ------------Elements without Keys ---------------- "; echo "<br>"; $array = array( "value1", "value2", 5 => "value3", "value4", ); var_dump($array); ?>
Output
PHP ***Associative Array*** Example ------------Elements without Keys ---------------- array(4) { [0]=> string(6) "value1" [1]=> string(6) "value2" [5]=> string(6) "value3" [6]=> string(6) "value4" }
Traversing through Array, Count, and Printing of Associative Array Example:
<?php echo "PHP ***Associative Array*** Example"; echo "<br>"; echo " ------------Ist method ------------------ "; echo "<br>"; $luxuryCars = array( "luxuryCar1Name" => "Volvo", "luxuryCar2Name" => "Audi,", "luxuryCar3Name" => "Ferrari" ); echo "I would like to have " . $luxuryCars["luxuryCar1Name"] . ", " . $luxuryCars["luxuryCar2Name"] . " and " . $luxuryCars["luxuryCar3Name"] . "."; echo "<br>"; echo "<br>"; echo " ------------2nd method ------------------ "; echo "<br>"; $luxuryCars["luxuryCar1Name"] = "Volvo"; $luxuryCars["luxuryCar2Name"] = "Audi,"; $luxuryCars["luxuryCar3Name"] = "Ferrari"; echo "I would like to have " . $luxuryCars["luxuryCar1Name"] . ", " . $luxuryCars["luxuryCar2Name"] . " and " . $luxuryCars["luxuryCar3Name"] . "."; echo "<br>"; echo "<br>"; echo " ------------Traversing PHP Associative Array ------------------ "; echo "<br>"; $luxuryCars = array( "luxuryCar1Name" => "Volvo", "luxuryCar2Name" => "Audi", "luxuryCar3Name" => "Ferrari" ); foreach ($luxuryCars as $xkey => $x_value) { echo "" . $xkey . " == " . $x_value; echo "<br>"; } echo "<br>"; echo " ------------ PHP Count Function over Associative Array ------------------ "; echo "<br>"; $luxuryCars = array( "luxuryCar2Name" => "Audi", "luxuryCar3Name" => "Ferrari" ); echo count($luxuryCars); ?>
Output
PHP ***Associative Array*** Example ------------Ist method ------------------ I would like to have Volvo, Audi, and Ferrari. ------------2nd method ------------------ I would like to have Volvo, Audi, and Ferrari. ------------Traversing PHP Associative Array ------------------ luxuryCar1Name == Volvo luxuryCar2Name == Audi luxuryCar3Name == Ferrari ------------ PHP Count Function over Associative Array ------------------ 2
This concludes the PHP Associative Array lesson. In the next lesson, you will learn about PHP String.