What is an associative array in php give example?


PHP 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:

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");
echo "Peter is " . $age['Peter'] . " years old.";
?>

Try it Yourself »


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:

Example

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

foreach($age as $x => $x_value) {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "
";
}
?>

Try it Yourself »



Complete PHP Array Reference

For a complete reference of all array functions, go to our complete PHP Array Reference.

The reference contains a brief description, and examples of use, for each function!


PHP Exercises



View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Associative arrays are used to store key value pairs. For example, to store the marks of different subject of a student in an array, a numerically indexed array would not be the best choice. Instead, we could use the respective subject’s names as the keys in our associative array, and the value would be their respective marks gained.

    Example:
    Here array() function is used to create associative array.

    $student_one = array("Maths"=>95, "Physics"=>90,  

                      "Chemistry"=>96, "English"=>93,  

                      "Computer"=>98); 

    $student_two["Maths"] = 95; 

    $student_two["Physics"] = 90; 

    $student_two["Chemistry"] = 96; 

    $student_two["English"] = 93; 

    $student_two["Computer"] = 98; 

    echo "Marks for student one is:\n"

    echo "Maths:" . $student_two["Maths"], "\n"

    echo "Physics:" . $student_two["Physics"], "\n"

    echo "Chemistry:" . $student_two["Chemistry"], "\n"

    echo "English:" . $student_one["English"], "\n"

    echo "Computer:" . $student_one["Computer"], "\n"

    ?> 

    Output:

    Marks for student one is:
    Maths:95
    Physics:90
    Chemistry:96
    English:93
    Computer:98
    

    Traversing the Associative Array:
    We can traverse associative arrays using loops. We can loop through the associative array in two ways. First by using for loop and secondly by using foreach.

    Example:
    Here array_keys() function is used to find indices names given to them and count() function is used to count number of indices in associative arrays.

    $student_one = array("Maths"=>95, "Physics"=>90,  

                      "Chemistry"=>96, "English"=>93,  

                      "Computer"=>98); 

    echo "Looping using foreach: \n"

    foreach ($student_one as $subject => $marks){ 

        echo "Student one got ".$marks." in ".$subject."\n"

    echo "\nLooping using for: \n"

    $subject = array_keys($student_one); 

    $marks = count($student_one);  

    for($i=0; $i < $marks; ++$i) { 

        echo $subject[$i] . ' ' . $student_one[$subject[$i]] . "\n"

    ?>  

    Output:

    Looping using foreach: 
    Student one got 95 in Maths
    Student one got 90 in Physics
    Student one got 96 in Chemistry
    Student one got 93 in English
    Student one got 98 in Computer
    
    Looping using for: 
    Maths 95
    Physics 90
    Chemistry 96
    English 93
    Computer 98
    

    Creating an associative array of mixed types

    $arr["xyz"] = 95; 

    $arr[100] = "abc"

    $arr[11.25] = 100; 

    $arr["abc"] = "pqr"

    foreach ($arr as $key => $val){ 

        echo $key."==>".$val."\n"

    }  

    ?> 

    Output:

    xyz==>95
    100==>abc
    11==>100
    abc==>pqr
    

    PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples.


    Which is a example of associative array *?

    Example. $salary=array("Sonoo"=>"550000","Vimal"=>"250000","Ratan"=>"200000"); echo "Sonoo salary: ".

    What is array associative array with syntax with example?

    An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

    What is an array of arrays in PHP given an example?

    Introduction to PHP multidimensional array A multidimensional array is an array that has more than one dimension. For example, a two-dimensional array is an array of arrays. It is like a table of rows and columns. In PHP, an element in an array can be another array.

    What is meant by associative array?

    In computer science, an associative array, map, symbol table, or dictionary is an abstract data type that stores a collection of (key, value) pairs, such that each possible key appears at most once in the collection.