How to use arrays in PHP

Arrays are lists of values grouped under a common name.

You can define an empty array in 2 different ways:

$list = [];

$list = array();

An array can be initialized with values:

$list = [1, 2];

$list = array(1, 2);

Array can hold values of any type:

$list = [1, 'test'];

Even other arrays:

$list = [1, [2, 'test']];

You can access the element in an array using this notation:

$list = ['a', 'b'];
$list[0]; //'a' --the index starts at 0
$list[1]; //'b'

Once an array is created, you can append values to it in this way:

$list = ['a', 'b'];
$list[] = 'c';

/*
$list == [
  "a",
  "b",
  "c",
]
*/

You can use array_unshift() to add the item at the beginning of the array instead:

$list = ['b', 'c'];
array_unshift($list, 'a');

/*
$list == [
  "a",
  "b",
  "c",
]
*/

Count how many items are in an array using the built-in count() function:

$list = ['a', 'b'];

count($list); //2

Check if an array contains an item using the in_array() built-in function:

$list = ['a', 'b'];

in_array('b', $list); //true

If in addition to confirming existance you need the index, use array_search():

$list = ['a', 'b'];

array_search('b', $list) //1

Useful PHP array functions

As with strings and numbers, PHP provides lots of very useful functions for arrays. We’ve seen count(), in_array(), array_search(), let’s see some more:

  • is_array() to check if a variable is an array
  • array_unique() to remove duplicate values from an array
  • array_search() to search a value in the array and returns the key
  • array_reverse() to reverse an array
  • array_reduce() to reduce an array to a single value using a callback function
  • array_map() to apply a callback function to each item in the array. Typically used to create a new array by modifying the values of an existing array, without altering that.
  • array_filter() to filter an array to a single value using a callback function
  • max() to get the maximum value contained in the array
  • min() to get the minimum value contained in the array
  • array_rand() to get a random item from the array
  • array_count_values() to count all the values in the array
  • implode() to turn an array into a string
  • array_pop() to remove the last item of the array and return its value
  • array_shift() same as array_pop() but removes the first item instead of the last
  • sort() to sort an array
  • rsort() to sort an array in reversing order
  • array_walk() similarly to array_map() does something for every item in the array, but in addition it can change values in the existing array

Associative arrays

So far we’ve used arrays with an incremental, numeric index: 0, 1, 2…

You can also use arrays with named indexes (keys), and we call them associative arrays:

$list = ['first' => 'a', 'second' => 'b'];

$list['first'] //'a'
$list['second'] //'b'

We have some functions especially useful for associative arrays:

  • array_key_exists() to check if a key exists in the array
  • array_keys() to get all the keys from the array
  • array_values() to get all the values from the array
  • asort() to sort an associative array by value
  • arsort() to sort an associative array in descending order by value
  • ksort() to sort an associative array by key
  • krsort() to sort an associative array in descending order by key

See all array-related functions here https://www.php.net/manual/en/ref.array.php

Download my free PHP Handbook!