23 Oct

PHP Tutorial: Using Arrays

Arrays are variables that are capable of holding multiple values, a group of data. These are heavily used in development, so in this beginner’s guide I’ll show how to declare, access and manipulate arrays in PHP.

PHP supports indexed, associative, and multidimensional array types.

Declaring Arrays in PHP

Arrays can be defined using the array() function, or from PHP 5.4 you can also just use [ ]. When declaring an indexed array,  indexes are numeric and the first item will start at index 0 and increment with each subsequent element.

The below snippet shows how we can declare various types of arrays:

We can add values to an array like:

Accessing Array Values

Reading values from an array, whether directly for a specific element or iterating through the elements is easily done with PHP.

Here’s how we can access elements in the above declared arrays:

When working with arrays, never assume a variable is actually an array or if specific keys exist in the array to avoid your script throwing an error.

We can test these conditions easily using is_array and array_key_exists functions:

Sorting Arrays in PHP

PHP gives us quite a few useful functions to help sort arrays.
Here I’ll briefly cover the functions I find I use the most for array sorting.

sort() and rsort()
Sort values of an indexed (ie non-associative) array, use sort() for ascending and rsort() for descending.

asort() and arsort()
Sort an associative array by values, use asort() for ascending and arsort() for descending.

ksort() and krsort()
Sort an associative array by keys, use ksort() for ascending and krsort() for descending.

Shuffle array elements into a random order.

Returns a new array containing items in reverse order from a given array.

Slicing, Shifting and Popping PHP Arrays

We can grab a slice of an array starting at a specified offset, for a specified length (or if we don’t specify a length it will grab from the offset to the end of the array) using array_slice():
array_slice ( array $array , int $offset [, int $length = NULL] )

Similarly, we can grab the first item of an array and remove it from the array at the same time, using array_shift(). As the item is removed from the array, any remaining items will have their numerical index reduced by 1, but associative keys aren’t touched.

If we want to grab the last item from the array instead and have it removed from the array also, we can use array_pop(). Remaining array items are left untouched.

Strings and Arrays: Implode and Explode

We can manipulate strings and arrays using these 2 useful functions.

implode() will combine items from an array into a string, optionally separated by a specified string. explode() will split a string into array items, splitting wherever a specified string is found within the main string (and we can optionally set a limit on the number of splits).


Other PHP Array Functions

Creates a new array based on the values of the given array, but without duplicate values:

in_array($needle, $haystack)
Returns true or false whether a value exists within an array:

We’ve only just touched on some of the more commonly used PHP Array functions, there are many more documented in the PHP Manual: Array Functions.

I hope you’ve found this guide useful, and if you feel I’ve missed something or have any feedback, please get in touch via the comments below – cheers.

Feature photo by: Walt Stoneburner.