Difference between indexed array and associative array in php?
An array is a collection of objects that contain a group of variables stored under the same name. All the elements belong to the same data type, i.e. string, integers, or lists. Keys are unique in the case of both indexed and associative arrays.
1. Example:
Code:
<?php // Declaring an array $arr = array(1, 2, 3, 4, 5); echo('Array1 : '); // Print the array print_r($arr); echo"<br/>"; // Declaring an array $arr = array( "Java" => "Spring Boot", "Python" => "Django", "PHP" => "CodeIgniter" ); // Assigning values print("Array2 : "); print_r($arr); ?>
Output:
Array1 : Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array2 : Array ( [Java] => Spring Boot [Python] => Django [PHP] => CodeIgniter )
2. Difference between Indexed array and associative array:
Indexed Array | Associative Array |
The keys of an indexed array are integers which start at 0. | Keys may be strings in the case of an associative array. |
They are like single-column tables. | They are like two-column tables. |
They are not maps. | They are known as maps. |