Originally published on CodingCush.
A PHP array is a single variable that holds many values at once. Instead of three separate variables called $fruit1, $fruit2 and $fruit3, you store all three fruits in one $fruits array and reach each one by position or name. In this guide you will learn the three types of PHP arrays, indexed, associative and multidimensional, plus how to loop through them, add and remove items, and use the everyday array functions. Every example here was run on PHP 8.3.32, so the outputs shown are real.
What Is a PHP Array?
A normal variable holds one value: a string holds one piece of text, a number holds one number. That works fine until you need a list, like the names of five students. An array stores the whole list under one name. Think of a variable as a single pocket and an array as a bag with many labelled pockets. If variables still feel new, our guide to PHP variables, data types and echo in PHP covers them first. The official PHP manual page on arrays calls an array an ordered map.
Indexed Arrays: Lists with Numbered Positions
An indexed array gives each value a number, starting from zero. The first item sits at position 0, the second at 1, and so on.
$fruits = ["apple", "banana", "mango"];
echo $fruits[0] . "\n";
echo $fruits[2] . "\n";
echo count($fruits) . "\n";
Output:
apple
mango
3
The square brackets hold the index you want. $fruits[0] gives "apple" because counting starts at zero, and $fruits[2] gives "mango". count() tells you the array holds 3 items. Use an indexed array for a plain list where order matters: colours, scores, or lines of a file.
Associative Arrays: Lists with Named Keys
An associative array works the same way, except you choose the keys, and the keys are usually words. Instead of remembering that position 0 is the name, you just ask for the value by its name.
$student = ["name" => "Meera", "age" => 21, "city" => "Delhi"];
echo $student["name"] . "\n";
echo $student["city"] . "\n";
Output:
Meera
Delhi
Here "name", "age" and "city" are the keys, and the arrow => pairs each key with its value. Reading $student["name"] is much clearer than reading $student[0]. Associative arrays are the natural choice for a single record, like a user profile.
The Arrays page in the official PHP manual.
Multidimensional Arrays: Arrays Inside Arrays
A multidimensional array is an array that holds other arrays. Think of it as a table: each row is itself an array, so you need two keys to reach a value.
$students = [
["name" => "Meera", "marks" => 88],
["name" => "Arjun", "marks" => 95],
];
echo $students[0]["name"] . " scored " . $students[0]["marks"] . "\n";
echo $students[1]["name"] . " scored " . $students[1]["marks"] . "\n";
Output:
Meera scored 88
Arjun scored 95
$students[0] is the first inner array, the one about Meera, and ["marks"] picks the marks field from it. Read the brackets left to right: first the row, then the field inside it. Two levels cover most real work, such as a list of users or rows from a database. Our guide to search and pagination in PHP and MySQL loops through exactly those rows.
Looping Through Arrays with foreach
Arrays and loops are best friends. A foreach loop walks through an array one item at a time. The same pattern shows up when you read form data, because PHP hands you form fields as an array too. Our guide to handling HTML forms in PHP with GET vs POST shows those arrays in action.
foreach ($fruits as $fruit) {
echo $fruit . "\n";
}
Output:
apple
banana
mango
On each round, $fruit holds the next value. When you also need the key, foreach gives you both at once:
foreach ($student as $key => $value) {
echo $key . ": " . $value . "\n";
}
Output:
name: Meera
age: 21
city: Delhi
The $key => $value form hands you the key and the value together. Use it to print a user profile or build an HTML table from database rows.
Adding and Removing Elements
Arrays in PHP grow and shrink as your program runs.
$fruits[] = "orange";
array_push($fruits, "grapes");
echo count($fruits) . "\n";
array_pop($fruits);
echo $fruits[3] . "\n";
unset($fruits[1]);
print_r($fruits);
Output:
5
orange
Array
(
[0] => apple
[2] => mango
[3] => orange
)
$fruits[] = "orange" appends one item, and array_push() does the same, so count() prints 5. Then array_pop() removes "grapes", and $fruits[3] is "orange" again. Finally unset() deletes "banana" at key 1. Notice the gap in the print_r() output: the keys are now 0, 2 and 3, because unset() removes the value but does not renumber the keys. Call array_values() for tidy keys from zero again.
Handy Array Functions: count(), in_array() and array_keys()
PHP ships with many built in array functions. The official PHP manual list of array functions documents them all, but these three cover a surprising amount of everyday work.
$basket = ["apple", "banana", "mango"];
echo count($basket) . "\n";
var_dump(in_array("banana", $basket));
var_dump(in_array("kiwi", $basket));
print_r(array_keys($student));
Output:
3
bool(true)
bool(false)
Array
(
[0] => name
[1] => age
[2] => city
)
count() tells you the array holds 3 items. in_array() answers a yes or no question: is "banana" in the basket? bool(true) means yes, and bool(false) for "kiwi" means no. array_keys() returns a new array of just the keys of $student. One detail: in_array() uses loose comparison by default, so the string "21" would match the number 21. Pass true as a third argument when the type must match too.
Indexed vs Associative vs Multidimensional
All three types are the same tool with different keys.
| Type | Keys | Best for | Example |
|---|---|---|---|
| Indexed | Numbers from 0 | Simple lists | ["apple", "banana", "mango"] |
| Associative | Names you choose | One record | ["name" => "Meera", "city" => "Delhi"] |
| Multidimensional | Numbers or names, nested | Tables, lists of records | [["name" => "Meera", "marks" => 88], ["name" => "Arjun", "marks" => 95]] |
When you are unsure, ask one question: do my items need names? If the order is enough, use an indexed array. If each value needs a label, use an associative array. If you have many records with named fields, nest associative arrays inside an indexed one.
Common Mistakes and Troubleshooting
Counting from 1 instead of 0. The first item is at index 0, so in a three item array the last valid index is 2. Asking for $fruits[3] gives the warning "Undefined array key 3".
Using a number key on an associative array. $student[0] does not give the first value, because the keys are names, not numbers. Use $student["name"], or loop with foreach.
Forgetting that unset() leaves a gap. After unset($fruits[1]) the keys are 0, 2 and 3, not 0, 1 and 2. A for loop counting from 0 will hit the missing key, so use foreach, or rebuild the keys with array_values().
Changing a value inside foreach and wondering why nothing changed. By default foreach gives you a copy of each value, so $fruit = "kiwi" inside the loop changes only the copy. Add an ampersand, foreach ($fruits as &$fruit), to change the real array.
Expecting count() to count nested items. count() counts only the top level by default, so a list of two students returns 2, not 4. Pass COUNT_RECURSIVE as a second argument if you need every nested value counted.
Frequently Asked Questions
What is an array in PHP?
A PHP array is a variable that stores many values under one name. Each value has a key, either a number starting from zero or a name you choose.
What is the difference between indexed and associative arrays?
An indexed array uses numbers as keys, starting from 0, while an associative array uses names you choose, such as "name" or "city". Use indexed arrays for plain lists, associative arrays for records with labelled fields.
How do I count items in a PHP array?
Use count($array). It returns the number of items at the top level of the array.
Can a PHP array hold different data types?
Yes. One array can hold strings, numbers, booleans and even other arrays at the same time.
How do I check if a value exists in a PHP array?
Use in_array($value, $array). It returns true when the value is present and false when it is not. Pass true as a third argument for a strict type check.
What happens if I access an array key that does not exist?
PHP 8 gives a warning, "Undefined array key", and the result is null. Check the key exists first with isset($array[$key]) or array_key_exists($key, $array).
Conclusion
Arrays turn scattered variables into tidy lists, named records and tables, all under one name. Start with indexed arrays for simple lists, reach for associative arrays when your values need names, and nest them for a list of records. Then practise three habits: loop with foreach, check with count() and in_array(), and remember that counting starts at zero.

Top comments (0)