Like most programming languages, PHP lets you create arrays. An array is a special type of variable that can hold many values at once, all accessible via a single variable name. Arrays are very useful whenever you need to work with large amounts of data — such as records from a database — or group related data together.
In this article, you’ll:
- Learn how PHP arrays work, and why they’re useful
- Look at the difference between indexed arrays and associative arrays, and
- Learn how to create arrays within your PHP scripts.
How arrays work
As you’ve seen already, arrays are variables that can hold more than one value. Here are some more key facts about arrays in PHP:
- An array can hold any number of values, including no values at all.
- Each value in an array is called an element.
- You access each element via its index, which is a numeric or string value. Every element in an array has its own unique index.
- An element can store any type of value, such as an integer, a string, or a Boolean. You can mix types within an array — for example, the first element can contain an integer, the second can contain a string, and so on.
- An array’s length is the number of elements in the array.
- An array element’s value can itself be an array. This allows you to create multidimensional arrays.
Why arrays are useful
Arrays in PHP offer many benefits, including the following:
- They’re easy to manipulate. It’s easy to add or remove elements in an array, as well as read or change the value of an element.
- It’s easy to work with many values at once. You can easily loop through all elements in an array, reading or changing each element’s value as you move through the loop.
- PHP gives you many handy array-related functions. For example, you can sort array elements quickly and easily; search arrays for particular values or indices; and merge arrays together.
Indexed arrays and associative arrays
PHP lets you create 2 types of array:
- Indexed arrays have numeric indices. Typically the indices in an indexed array start from zero, so the first element has an index of
0, the second has an index of
1, and so on. Usually, you use an indexed array when you want to store a bunch of data in a certain order.
- Associative arrays have string indices. For example, one element of an associative array might have an index of
"name", while another element has an index of
"age". The order of the elements is usually unimportant. Typically, you use an associative array when you want to store records of data, much like using a database.
In fact, PHP doesn’t make any internal distinction between indexed and associative arrays. You can even mix numeric and string indices within the same array if you like. Most of the time, however, it helps to think of indexed and associative arrays as different types of arrays. Furthermore, many PHP array functions are designed to work with either indexed or associative arrays.
An associative array is sometimes referred to as a hash, and its indices are often called keys.
How to create an array in PHP
It’s easy to create an array within a PHP script. To create an array, you use the
$myArray = array( values );
To create an indexed array, just list the array values inside the parentheses, separated by commas. The following example creates an indexed array of movie director names and stores it in a variable called
$directors = array( "Alfred Hitchcock", "Stanley Kubrick", "Martin Scorsese", "Fritz Lang" );
When creating an indexed array, PHP automatically assigns a numeric index to each element. In the above example,
"Alfred Hitchcock" is given an index of
"Stanley Kubrick" has an index of
1, and so on.
To create an associative array, you pair each value with the index that you want to use for that value. To do this you use the
index => value
The following example creates an associative array with information about a movie, and stores it in a variable called
$movie = array( "title" => "Rear Window", "director" => "Alfred Hitchcock", "year" => 1954 );
By the way, to create an array with no elements, you can write:
$myArray = array();
In this tutorial you’ve looked at the concept of arrays in PHP, and how to create them. In future tutorials I’ll show how to do stuff with arrays, such as add and remove elements, read and change element values, sort values in any order, and lots more.
What if i’m trying to create an array of objects, how does that work?
i want to create an array with records from an ms sql server table
Matt Doyle says
Use PDO: https://www.php.net/manual/en/book.pdo.php