PHP Associative Array

In this lesson, you will learn about the Associative Array in PHP, its usage, and examples to better understand the topic.


PHP Associative Array

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.

Syntax

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:

  • Strings containing valid decimal integers will be cast to the integer type unless a + 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.
  • Floats are also cast to integers, meaning the fractional part is truncated. E.g., the 8.7 key will be placed below 8.
  • Bools are also cast to integers, i.e., the real key will be placed below 1 and the fake key below 0.
  • Null is cast to the empty string, i.e., the null key is placed under “”.
  • It is impossible to use arrays and items like keys. This will lead to a warning: unlawful type of offset.

PHP Type Casting Associative Array

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.

Type Casting Example

<?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.


Integer and String as Key Example

<?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.


Key as an Option Example

<?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.


Elements without Keys Example

<?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" }
Note
As you can notice in the result of the above example, the last value, ” value4,” was allocated the key 6. The reason for it is the largest integer key earlier that was 5.

PHP Associative Array Traversing through Array

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.