PHP Array

Summary: in this tutorial, you will learn about PHP array. We will discuss about the indexed array and associative array, and show you various techniques to manipulate arrays.

Introduction to PHP array

In PHP, an array is made up of elements that are related and shared the same characteristics. Each array’s element has a key and value.

The key in array can be a string or a number. You use a key to look up a value of an element. The value of an element can be a string, a number, a Boolean value, an object or even another array.

There are two types of arrays in PHP:

  • Indexed array: each element of an indexed array is referred to using a numeric index.
  • Associative array: each element of an associative array is referred to using a string. The associative array is also known as a map or hash.

PHP Array

Creating arrays

Unlike other programming languages such as C/C++, PHP does not require you to specify the size of array when you create it. PHP also does not require you to declare array variable before using it.

To create an array, you specify a key inside a pair of square brackets followed by array variable and assign a value to it as follows:

We have defined an array of languages that contains three elements. It is an indexed array because the values of its elements are referred by numeric values. If we use numeric values as the keys of an array, we can omit them when we initialize it as follows:

To create an associative array, you need to specify the keys of its elements explicitly. The following example defines the colors associative array:

Creating arrays using array() function

PHP provides array() function that creates an indexed or associate array. The following example creates the languages array that contains three elements:

To create an associative array, we can use array() function with explicit keys defined for each element as following:

From PHP 5.4, you can use short syntax [] instead of the array() function, for example:

Creating arrays using range() function

PHP provides the range() function to help you create a range array and fill it with elements that are in a range of values. The following is the range() function:

You specify the range with low and high values, and an optional parameter called step that indicates the distance between element’s values. For example, to create an array of 10 integers, you can use the range() function as follows:

If you want to create an array that contains only even numbers from 2 to 10, you can use the range() function as follows:

Displaying arrays

It is very handy to inspect an entire array using the print_r() function. See the following example:

 

PHP print_r displays array

Another way to display more detail about arrays is using the var_dump() function:

PHP var_dump displays array

Counting array elements

In order to find the number of elements in an array, you use the count() function.

Testing if a variable is an array

PHP provides you with a function named is_array() that tests if  the type of a variable is array. The is_array() function returns true if a variable is an array, otherwise it returns false. Let’s take a look at the following example:

Adding / Removing elements

In order to add a new element to an array, you need to assign a value to the new element, by specifying either numeric or string index based on the array type.

PHP also provides a built-in function array_push() that adds one or more elements at the end of the array. The  array_push() function returns true on success and false on failure. See the following example:

To add an element at the begin of an array, you can use the array_unshift() function.

To remove the last element from an array, you use the the array_pop() function. The  array_pop() function returns the removed element:

Searching for array elements

Testing if the array contains an element

PHP provides the in_array() function that checks whether an element is in array or not. You pass the element and array arguments to the function. If the element is found, the function returns true otherwise it returns false.

There is a third optional argument named strict. If you pass this argument as true, the function will check not only the value but also the data type of the element.

The second check was failed because the array element is integer type while the element to check is string type.

Check if a key exists in the keys of an associative array

To check whether a key exists in an associative array, you use the array_key_exists() function. The function returns true if the key is found in the array, otherwise returns false.

Getting keys and values in associative arrays

If you want to get a set of specific keys in an associative array, you can use the array_keys() function. The array_keys() function returns an array that contains all the keys of the array.

If you want to get all values of an associative array, you use the array_values() function.

Looping over arrays

PHP provides a very special kind of looping statement called foreach that allows you to loop over arrays. The foreach statement only works with arrays and objects.

Looping over indexed arrays

In order to loop over an indexed array, you use the following syntax:

The following example loops over an index array of programming languages and display each element’s value:

Loop over associative arrays

To loop over associative arrays, you use the following syntax:

The following example illustrates how to loop over the $address associative array:

Sorting Arrays

Sorting indexed arrays

PHP provides a pair of functions that allow to sort indexed arrays: sort() and rsort(). The sort() function sorts an index arrays in ascending order, while rsort() function sorts an indexed array in descending order. To sort an indexed array, you need to pass the array to the function as the following example:

PHP array sort rsort

Sorting associative arrays

To sort associative arrays, you use a pair of functions: asort() and arsort().

  • The  asort() function sorts an associative array by its values in ascending order.
  • The  arsort() function sorts an associative array by its values in descending order.

Both functions preserve the association between each element’s  key and its value.

Notice that if you apply the sort() or rsort() function to an associative array, the associative array is converted into an indexed array therefore you lose the link between each element’s key and its value.

To sort an associative array by its keys, we can use a pair of functions: ksort() and krsort().

  • The ksort() function sorts an associative array by its keys in ascending order.
  • The krsort() function sorts an associative array by its keys in descending order.

Let’s take a look at the following example:

Merging arrays

To merge 2 or more arrays into one array, you use the array_merge() function. The following example demonstrates how to merge 2 integer arrays:

Converting an indexed array into a list of variables

PHP provides a very handy function named list() that helps you convert an indexed array into a list of variables. The following example demonstrates how to use the list() function.

Converting between an array and a string

Convert a string into an array

To convert a string into an array, you use the explode() function. The explode() function splits the string based on the specified delimiter and returns an array that contains elements, which are substrings produced by the splitting operation. The following example converts a string into an array using a comma ( ,) as the delimiter. The result is an array of strings.

Convert an array into a string

You use the implode() function to convert an array into a string as follows:

In this tutorial, we have introduced you the PHP array and various built-in array functions that allows you manipulate arrays effectively.

  • Was this tutorial helpful ?
  • YesNo