What is Array in PHP?
In PHP, an array is a powerful data structure that stores multiple values in a single variable. Unlike simple variables that hold only one value, arrays allow you to group related data together, which can be accessed by keys or indexes. This makes managing collections of data easier and more efficient.
PHP arrays are actually ordered maps that associate values to keys. Keys can be integers or strings, and values can be any type, including other arrays, enabling complex, multidimensional data structures.
PHP Arrays: An array is a data structure that stores one or more similar type of values in a single value. For example, if you want to store 100 numbers then instead of defining 100 variables it’s easy to define an array of 100 lengths.

Why Use Arrays in PHP?
Arrays offer a flexible way to:
-
Store multiple values in one variable.
-
Group related data (like a list of products, users, or settings).
-
Access data conveniently via keys or indexes.
-
Build complex data structures like hashes, dictionaries, stacks, or queues.
Types Of Arrays in PHP 8?
In PHP 7, there are three types of arrays:
- Indexed arrays – Arrays with a numeric index
- Associative arrays – Arrays with named keys
- Multidimensional arrays – Arrays containing one or more arrays
1. Numerically Indexed Arrays
Let’s assume that you’ve been tasked with creating a simple website for a local office supply company. And you’re currently working on the section devoted to paper. One way to manage the various items of stock in this category would be to place them in a numeric array. Basically, You can see the simplest way of doing. So, in the Example given below.
<?php $paper[] = "Copier"; $paper[] = "Inkjet"; $paper[] = "Laser"; $paper[] = "Photo"; print_r($paper); ?>

In this example, each time you assign a value to the array $paper, the first empty location within that array is used to store the value, and a pointer internal to PHP is incremented to point to the next free location, ready for future insertions. As your familiar print_r function is used to verify that the array has been correctly populated. Here, It prints out the following:
Array ( [0] => Copier [1] => Inkjet [2] => Laser [3] => Photo )
The previous code could also have been written as shown in Example 6-2, where the exact location of each item within the array is specified. But, as you can see, that approach requires extra typing and makes your code harder to maintain. So, if you want to insert or remove supplies from the array. So, unless you wish to specify a different order, it’s usually better to simply let PHP handle the actual location numbers.
Example 6-2. Adding items to an array using explicit locations
<?php $paper[0] = "Copier"; $paper[1] = "Inkjet"; $paper[2] = "Laser"; $paper[3] = "Photo"; print_r($paper); ?>
The output from these examples is identical, but you are not likely to use print_r in a developed website. So, Example 6-3 shows how you might print out the various types of paper the website offers using a for a loop.
Example 6-3. Adding items to an array and retrieving them
<?php $paper[] = "Copier"; $paper[] = "Inkjet"; $paper[] = "Laser"; $paper[] = "Photo"; for ($j = 0 ; $j < 4 ; ++$j) echo "$j: $paper[$j]<br>"; ?>
This example prints out the following:
0: Copier 1: Inkjet 2: Laser 3: Photo
So far, you’ve seen a couple of ways in which you can add items to an array and one way
of referencing them, but PHP offers many more—which I’ll get to shortly. But first, we’ll
look at another type of array
Get The Length of an Array – The count() Function
The functioncount()
is used to return the length or the number of elements of an array:
<?php $cars = array("Volvo", "BMW", "Toyota"); echo count($cars); ?>
2. Associative arrays in PHP 7
Keeping track of array elements by index works just fine, but can require extra work in terms of remembering which number refers to which product. It can also make code hard for other programmers to follow.
This is where associative arrays come into their own. Using them, you can reference the items in an array by name rather than by number.
Example:
$stock = [
"paper" => "Copier",
"ink" => "Laser",
"photo" => "Glossy",
];
echo $stock["ink"]; // Outputs: Laser
Associative arrays let you map meaningful keys to values, aiding maintenance and reducing errors.
***You can read about associative arrays on the next page***.
3. Multidimensional Arrays
Arrays can hold other arrays as values to create complex data organizations.
Example:
$inventory = [
"paper" => ["Copier", "Inkjet", "Laser"],
"colors" => ["Red", "Blue", "Green"]
];
echo $inventory["paper"][1]; // Outputs: Inkjet
Common PHP Array Functions
-
count($array)
– Returns the number of elements. Its important PHP array function. -
array_merge($array1, $array2)
– Merges two or more arrays. -
unset($array[key])
– Removes an element without reindexing. -
array_values($array)
– Returns values reindexed numerically. -
Sorting functions like
sort()
,asort()
,ksort()
etc. to order arrays.
Example: Counting Array Elements
$cars = ["Volvo", "BMW", "Toyota"];
echo count($cars); // Outputs: 3
Additional Tips on PHP Arrays
-
Use square brackets
[]
to access or assign values to arrays. -
When assigning without a key, PHP automatically uses the next available integer index.
-
Keys that are strings or integers behave distinctly; note that string numeric keys are cast to integers where possible.
-
Use quotes when accessing array keys as strings to avoid undefined constant notices:
$array["key"]
instead of$array[key]
. -
Arrays preserve references when copied, meaning changes in one copy can affect the other if references are used.
-
PHP 7.1 and higher support array destructuring, allowing for unpacking arrays into variables easily.
-
The spread operator (
...
) can be used to unpack arrays into other arrays as of PHP 7.4.
Example: Array Destructuring
$source = ['apple', 'banana', 'cherry'];
list($fruit1, $fruit2, $fruit3) = $source;
echo $fruit1; // apple
echo $fruit2; // banana
echo $fruit3; // cherry
Summary
PHP arrays are versatile and essential data structures that help efficiently store and manipulate collections of data. They support numeric and associative keys, can be multidimensional, and come with rich built-in functions to facilitate common tasks like counting, merging, sorting, and iterating.
By mastering arrays in PHP, you can handle complex data scenarios effectively and write cleaner, more maintainable code.
Also read: Best way for freelancers to make money.
<< Prev Next >>