Facebook Page Twitter Page LinkedIn Page
× PHP Tutorial PHP Advanced PHP Resources


In PHP, associative array is used to store data in key-value pairs. It is used to hold multiple key-value pairs in a single variable.

Create an Array

An associative array can be created using array() keyword. It can be initialized at the time of creation by specifying key-value pairs within the array() keyword and separated by comma(,) or it can be initialized later by specifying value using key.

Syntax

//Creating an empty associative array.
$MyArray = array();  

//Creating an associative array with initialization.
$MyArray = array(key1=>value1, key2=>value2, ...); 

//initialization after creation of array
$MyArray[key] = value;

Access element of an associative array

An element of an Array can be accessed with it's key. Below mentioned example illustrates the way of accessing data in an associative array.

<?php
  //creating an array with 3 elements
  $MyArray = array(101=>"Red", 102=>"Blue", 103=>"Green");

  //Add 4th and 5th elements
  $MyArray[104] = "Black";
  $MyArray[105] = "White";
  
  echo "101:" . $MyArray[101]."\n";
  echo "103:" . $MyArray[103]."\n";
  echo "105:" . $MyArray[105]."\n";
?>

The output of the above code will be:

101:Red
103:Green
105:White

Change the value of a key

The value of any key of an associative array can be changed using its key and assigning a new value.

<?php
  $MyArray = array(101=>"Red", 102=>"Blue", 103=>"Green");

  //changing the value of a key
  $MyArray[101] = "Yellow";
  
  echo "101:" . $MyArray[101]."\n";
  echo "102:" . $MyArray[102]."\n";
  echo "103:" . $MyArray[103]."\n";
?>

The output of the above code will be:

101:Yellow
102:Blue
103:Green

Loop over an Associative Array

By using foreach loop, each elements of an associative array can be accessed.

Foreach Loop over an Array

In the below example, foreach loop is used to access all elements of an associative array.

<?php
  $MyArray = array();

  $MyArray[101] = "Red";
  $MyArray[102] = "Blue";
  $MyArray[103] = "Green";  
  
  echo "MyArray contains: \n";
  foreach($MyArray as $x => $x_value)
    echo "Key=" . $x . ", Value=" . $x_value."\n";
?>

The output of the above code will be:

MyArray contains: 
Key=101, Value=Red
Key=102, Value=Blue
Key=103, Value=Green




PHP Array Functions
FunctionDescription
array()Creates an array
array_change_key_case()Changes all keys in an array to lowercase or uppercase
array_chunk()Splits an array into chunks of arrays
array_column()Returns the values from a single column in the input array
array_combine()Creates an array by using the elements from one "keys" array and one "values" array
array_count_values()Counts all the values of an array
array_diff()Compare arrays, and returns the differences (compare values only)
array_diff_assoc()Compare arrays, and returns the differences (compare keys and values)
array_diff_key()Compare arrays, and returns the differences (compare keys only)
array_diff_uassoc()Compare arrays, and returns the differences (compare keys and values, using a user-defined key comparison function)
array_diff_ukey()Compare arrays, and returns the differences (compare keys only, using a user-defined key comparison function)
array_fill()Fills an array with values
array_fill_keys()Fills an array with values, specifying keys
array_filter()Filters the values of an array using a callback function
array_flip()Flips/Exchanges all keys with their associated values in an array
array_intersect()Compare arrays, and returns the matches (compare values only)
array_intersect_assoc()Compare arrays and returns the matches (compare keys and values)
array_intersect_key()Compare arrays, and returns the matches (compare keys only)
array_intersect_uassoc()Compare arrays, and returns the matches (compare keys and values, using a user-defined key comparison function)
array_intersect_ukey()Compare arrays, and returns the matches (compare keys only, using a user-defined key comparison function)
array_key_exists()Checks if the specified key exists in the array
array_keys()Returns all the keys of an array
array_map()Sends each value of an array to a user-made function, which returns new values
array_merge()Merges one or more arrays into one array
array_merge_recursive()Merges one or more arrays into one array recursively
array_multisort()Sorts multiple or multi-dimensional arrays
array_pad()Inserts a specified number of items, with a specified value, to an array
array_pop()Deletes the last element of an array
array_product()Calculates the product of the values in an array
array_push()Inserts one or more elements to the end of an array
array_rand()Returns one or more random keys from an array
array_reduce()Returns an array as a string, using a user-defined function
array_replace()Replaces the values of the first array with the values from following arrays
array_replace_recursive()Replaces the values of the first array with the values from following arrays recursively
array_reverse()Returns an array in the reverse order
array_search()Searches an array for a given value and returns the key
array_shift()Removes the first element from an array, and returns the value of the removed element
array_slice()Returns selected parts of an array
array_splice()Removes and replaces specified elements of an array
array_sum() Returns sum of all values in the array.
array_udiff()Compare arrays, and returns the differences (compare values only, using a user-defined key comparison function)
array_udiff_assoc()Compare arrays, and returns the differences (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values)
array_udiff_uassoc()Compare arrays, and returns the differences (compare keys and values, using two user-defined key comparison functions)
array_uintersect()Compare arrays, and returns the matches (compare values only, using a user-defined key comparison function)
array_uintersect_assoc()Compare arrays, and returns the matches (compare keys and values, using a built-in function to compare the keys and a user-defined function to compare the values)
array_uintersect_uassoc()Compare arrays, and returns the matches (compare keys and values, using two user-defined key comparison functions)
array_unique()Removes duplicate values from an array
array_unshift()Adds one or more elements to the beginning of an array
array_values()Returns all the values of an array
array_walk()Applies a user function to every member of an array
array_walk_recursive()Applies a user function recursively to every member of an array
arsort() Sorts an associative array in descending order, according to the value.
asort() Sort an associative array in ascending order, according to the value.
compact()Create array containing variables and their values
count()Returns the number of elements in an array
current()Returns the current element in an array
each()Deprecated from PHP 7.2. Returns the current key and value pair from an array
end()Sets the internal pointer of an array to its last element
extract()Imports variables into the current symbol table from an array
in_array()Checks if a specified value exists in an array
key()Fetches a key from an array
krsort() Sorts an associative array in descending order, according to the key.
ksort() Sorts an associative array in ascending order, according to the key.
list()Assigns variables as if they were an array
natcasesort()Sorts an array using a case insensitive "natural order" algorithm
natsort()Sorts an array using a "natural order" algorithm
next()Advance the internal array pointer of an array
pos()Alias of current()
prev()Rewinds the internal array pointer
range()Creates an array containing a range of elements
reset()Sets the internal pointer of an array to its first element
rsort() Sorts an array in descending order.
shuffle()Shuffles an array
sizeof()Alias of count()
sort() Sorts an array in ascending order.
uasort() Sorts an associative array using a user-defined comparison function, according to the value.
uksort() Sorts an associative array using a user-defined comparison function, according to the key.
usort() Sorts an array using a user-defined comparison function.