Arrays

Published Jul 31, 2021Updated Jul 22, 2023
Contribute to Docs

An array can hold more than one value. In PHP, they’re stored as value pairs that in other languages would be called a dictionary or a hashtable. Keys can be strings or integers.

Syntax

There are several methods of declaring an array in PHP. The array() function can be used, either with key-value pairs, or with values alone. Single brackets, [...] can also be used in place of the array() keyword. If any key value is omitted, the key will be found by incrementing the largest prior integer key. If a key is repeated, the new value will overwrite the prior key.

<?php
// The last comma can be omitted
$array1 = array( "item 1" => "one", "item 2" => "two", "item 3" => "three", );
echo $array1["item 1"], ";", $array1["item 2"], ";", $array1["item 3"];
// Output: one;two;three
$array2 = array("one", "two", "three");
echo $array2[0], ";", $array2[1], ";", $array2[2];
// Output: one;two;three
$array3 = ["one", 5 => "two", "three"];
echo $array3[0], ";", $array3[5], ";", $array3[6];
// Output: one;two;three
$array4 = [5 => "one", 5.7 => "two", "5" => "three"];
echo $array4[5];
// Output: three
?>

When defining an array, the following key casts will occur:

  • Strings containing valid int types, unless preceded by a + sign, will be cast to an int type key. As in the above example "5" is treated as 5.
  • float types will be cast to int types, truncating the fractional part. As in the above example 5.7 is treated as 5.
  • bool types are cast to int types. true is stored as 1 and false stored as 0.
  • null will be cast as the empty string, "".
  • Arrays and objects cannot be used as keys and will result in an error: Illegal offset type.

Array Functions

Below is a list of selected array functions:

Arrays

array()
Returns an array that can be either indexed, associative or multidimensional.
array_chunk()
Splits an array into a number of specified chunks.
array_column()
Returns the values from a single column in the input array.
array_combine()
Returns a new array by combining two arrays, where one array represents keys and the other array represents values.
array_count_values()
Counts the occurrences of values in an array.
array_fill()
Fills an array with values and returns the filled array.
array_flip()
Interchanges the keys and values of an array.
array_intersect()
Returns the matching values of two or more arrays.
array_keys()
Returns all keys or a subset of the keys of a given array.
array_map()
Creates a new array by applying a callback function to each element of an existing array.
array_merge()
Merges one or more arrays to form a single array.
array_pad()
Pads an array to a specified length with a value.
array_pop()
Removes the last element of an array.
array_push()
Adds one or more element values to the end of an array, and returns the updated array.
array_rand()
Used to select one or more random keys from an array.
array_reduce()
Iterates through an array to produce a single value.
array_replace()
Used to replace the values of an array with values from one or more arrays.
array_reverse()
Reverses the items in an array and returns the reversed array.
array_search()
Searches an array for a given value and returns the first matching key for that value.
array_shift()
Pops an element off the beginning of an array.
array_splice()
Removes a set of elements from an array and replaces them with a new set of elements.
array_unique()
Returns an array without duplicate values.
array_values()
Returns all of the values in a given array.
compact()
Returns an array from variables and their values.
extract()
Uses an array of keys and values to create variables in the current symbol table.
range()
Returns an array that contains a series of elements based on the parameters given.
rsort()
Sorts a given array in descending order.
shuffle()
Randomizes the indexes of the elements in a given array.
sort()
Sorts an array in ascending order.

All contributors

Looking to contribute?

Learn PHP on Codecademy