Arrays in PHP: Indexed, Associative & Multidimensional
Arrays in PHP
An array is a single variable that holds many values. Instead of creating ten separate variables for ten student names, you store them all in one array. PHP supports indexed arrays, associative arrays (key/value pairs), and arrays nested inside arrays.
Indexed arrays
An indexed array stores values in order, numbered starting from 0.
<?php
// Create an indexed array.
$subjects = ["PHP", "MySQL", "HTML"];
echo $subjects[0]; // PHP (first item, index 0)
echo $subjects[2]; // HTML (third item, index 2)
// Add an item to the end.
$subjects[] = "CSS";
?>
Associative arrays
An associative array uses named keys instead of numbers, which makes data easier to read.
<?php
// Each value has a meaningful key.
$student = [
"name" => "Kabir",
"age" => 20,
"course" => "PHP & MySQL",
];
echo $student["name"]; // Kabir
echo $student["course"]; // PHP & MySQL
?>
Multidimensional arrays
You can put arrays inside arrays, which is useful for lists of records, such as rows from a database.
<?php
$students = [
["name" => "Anjali", "marks" => 88],
["name" => "Rohan", "marks" => 76],
];
// Read the marks of the first student.
echo $students[0]["marks"]; // 88
?>
Looping over arrays
foreach is the cleanest way to read every item.
<?php
$fruits = ["Apple", "Mango", "Banana"];
foreach ($fruits as $fruit) {
echo $fruit . "<br>";
}
?>
For associative arrays, capture both the key and the value:
<?php
$prices = ["pen" => 10, "book" => 60];
foreach ($prices as $item => $cost) {
echo "$item costs $cost<br>";
}
?>
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesUseful array functions
PHP ships with many helpful functions for arrays. A few you will use often:
<?php
$numbers = [5, 2, 9, 1];
echo count($numbers); // 4 -> number of items
array_push($numbers, 7); // add 7 to the end
$last = array_pop($numbers); // remove and return the last item
sort($numbers); // sort in ascending order
var_dump(in_array(9, $numbers)); // true -> is 9 present?
?>
Other handy functions include array_keys(), array_values(), array_merge(), and array_map().
Combining and searching
<?php
$a = ["red", "green"];
$b = ["blue", "yellow"];
$colors = array_merge($a, $b); // ["red", "green", "blue", "yellow"]
// Find the index of a value.
$position = array_search("blue", $colors); // 2
?>
Common mistakes
- Forgetting that indexes start at 0, so the first item is
$array[0], not$array[1]. - Accessing a key that does not exist, which produces a warning. Check with
isset()orarray_key_exists()first. - Confusing
sort()withasort()/ksort().sort()reindexes and discards keys; useasort()to keep associative keys. - Using
count()inside a loop condition repeatedly, which is wasteful. Store it in a variable when looping withfor.
FAQ
What is the difference between an indexed and an associative array?
Indexed arrays use numeric positions (0, 1, 2). Associative arrays use named keys you choose, like "name" or "age".
How do I count the items in an array?
Use count($array). For nested counting, pass COUNT_RECURSIVE as the second argument.
How do I check if a key exists?
Use isset($array["key"]) or array_key_exists("key", $array) to avoid undefined-key warnings.
Keep learning
Back to the PHP & MySQL hub, revisit Control Flow in PHP, or continue to Functions in PHP.
Work with real datasets and mentor feedback. Join the waitlist for the PHP course at Infoplanet in Jalgaon.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Control Flow in PHP: if, switch & Loops
Make PHP code decide and repeat with control flow: if/else, switch, the match expression, and the for, while, and foreach loops, with clear examples.
Handling Forms in PHP (GET & POST)
Handle HTML forms in PHP: the difference between GET and POST, reading input from the superglobals, validating data, and escaping output to prevent XSS.
Functions in PHP: Define, Call & Return
Understand PHP functions: defining and calling them, parameters with defaults, named arguments, return values, type hints, and variable scope, with examples.
