PHP Arrays
By: Zubair Idris Aweda
Tags: Web Development, Php Arrays, PHP, Arrays, Data Type

PHP is a server scripting language and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used, free, and efficient alternative to competitors such as Microsoft’s ASP. PHP 7 is the latest stable release.

Being a popular programming language, it allows for data manipulations with its many data types.

Getting Started

An array is a data structure that stores one or more similar type of values in a single value.

An array could be said to be a collection of data. It could be an empty collection like an empty box, or with just one item, like a car with just one person in it — it could take more you know, or with as many items as you want.

Arrays are of various types, numerically indexed arrays, associative arrays, multidimensional arrays.

Creating An Array

An array is created like any other data type in PHP, it is assigned to a variable.

An array can be created in many ways.

Other times you could want an array so simple yet too stressful to type. You could want an array of numbers or an array of English letters.

Range()

The range function helps autogenerate the content of the array given a starting and endpoint.

Printing The Array

So now we have our food array, it contains some foods — I know Cat is not a food or is it? Let’s keep going. We want to output our array to the screen on our browser or terminal.

We use the print_r function, it displays information about a variable, in this context an array, in a way that’s readable by humans. Read more about print_r here.

Now, you must wonder what the numbers in the array are, let’s see what they are in the next section.

Array Index

PHP arrays are made up of elements, each item in the array being regarded as an element, with each element having an index with which it can be referenced.

The indexes are auto-generated when you create the array, with the first element having an index of zero — PHP like other programming languages is zero-indexed but the index could be specified if you want.

PHP Array | Educative.io

So, the first element in the array has an index of 0, the second an index of 1 and it continues in this pattern.

To specify your indexes, simply create the array this way:

Now how do you print out only one item of your array? Keep calm and keep reading.

Printing An Array Element

So many times when you do not want to print out every element of your array, but only one.

You use the index to select the single item and echo it out.

Or you could even use it in a sentence like:

Outputting an array element | Carbon.now.sh

Other Array Functions

So in real-life applications, the array could grow large, that it could get hard to manage.

PHP gives us a bunch of functions to do that.

is_array()

PHP’s is_array() function tells whether the given variable is an array. It returns True if it is an array and False if not an array.

count()

PHP’s inbuilt count function tells us the number of elements any array has. This function is really helpful in deciding what to display in web apps. For example in a blog app, if the user has no posts or articles, display “No Articles Yet”, else display all the articles. So you use the count function to get the length.

This function is the same as the len() function in Python or the length() method in JavaScript.

array_sum()

array_sum() calculates the sum of all elements in the array.

In this example, the array was generated automatically with numbers from 1 to 10, and the sum was calculated using the array_sum() function.

sort()

If you want elements to be arranged from lowest to highest, then this function has is all you need as it returns the same array with its elements sorted.

rsort()

This function does opposite of the sort() function as the values are not sorted from highest to lowest.

shuffle()

Some other times, you could want to build a random quote generator or want to randomize questions in an online test.

You use the shuffle() to rearrange the content of the array at random.

The array is returned with its content in a new randomized order.

Note that on running this code, you could have an output that differs slightly from mine, this is because shuffle doesn’t rearrange in a specified manner. So running the code multiple times also will output different orders.

array_rand()

In some other cases, you just want a random value from an array, not shuffle the whole array, like in a game of dies, you want to output a random value between 1 and 6 every time the user plays.

This code outputs a random number in the given range every time it is run.

in_array()

in_array() helps you check if a given value exists in an array. It returns true if it exists and false if it does not exist.

array_count_values()

So let’s say you want to make a report on what type of cars people love. So you decide to build a little web app where users get to enter their favorite type of car.

Now at the end of your data collection, you have a long array of cars. You use array_count_values() to know how many times every element of the array was repeated.

The array returned is an associative array with the elements of the given array as keys and their counts as values.

array_unique()

And some other times, you just want to know how many unique elements you have in your array. array_unique() returns an array of only the unique values.

The returned array is:

And it shows that you have only 3 different cars in an array with 10 elements.

array_diff()

Getting the difference between two or more arrays can be easily done with the array_diff() function, the function returns an array of values that are not common to the given arrays.

The returned array, in this case, has just an element as the only element in fruits_a, not in fruits_b is “Banana”.

array_reverse()

array_reverse() returns the array with the elements arranged in reverse order without any sorting, just an order that opposes the order which they were before.

And so many other functions that will be discussed later.