Arrays in PHP (Indexed, Associative, Multidimensional)
In this lesson: Learn all 3 PHP array types with create, access, loop and print_r output — plus practical student-marks example. Hindi & English.
What is an array
An array is one variable that holds many values together, instead of making a separate variable for each. Want to store 30 students' marks? One array does it. PHP has two main kinds: indexed (numbered) and associative (named keys).
Indexed arrays
Keys are numbers, starting at 0.
<?php
$fruits = ["Apple", "Mango", "Banana"];
echo $fruits[0]; // Apple
echo $fruits[2]; // Banana
echo count($fruits); // 3
?>
The first item is [0], not [1]. count() tells you how many items are in the array.
Associative arrays
Here you choose meaningful text keys instead of numbers.
<?php
$student = [
"name" => "Aman",
"class" => 12,
"marks" => 88
];
echo $student["name"]; // Aman
echo $student["marks"]; // 88
?>
Associative arrays are perfect when each value needs a label, like a record with name, class and marks.
Looping with foreach
foreach is the cleanest way to go through an array.
<?php
$marks = ["Math" => 90, "Science" => 85];
foreach ($marks as $subject => $score) {
echo "$subject: $score\n";
}
?>
Science: 85
For a plain list use foreach ($arr as $value); for key-value pairs use foreach ($arr as $key => $value).
Multidimensional arrays
An array of arrays — like a table.
<?php
$students = [
["Aman", 88],
["Diya", 92]
];
echo $students[1][0]; // Diya
echo $students[1][1]; // 92
?>
$students[1][0] means: row 1 (second student), column 0 (name). This is how you store rows of related data.
Useful array functions
| Function | What it does |
|---|---|
| count($a) | How many items |
| $a[] = "x" | Add item at the end |
| in_array("x", $a) | Check if a value exists |
| sort($a) | Sort values ascending |
| array_keys($a) | Get all keys |
Common mistakes
- Forgetting arrays start at 0. The first item is index 0, so a 3-item array has no index 3.
- Using an undefined key.
$a["xyz"]when "xyz" was never set gives a warning. - Using a for loop where foreach is cleaner. For associative arrays, foreach avoids key mistakes.
- Mixing up value and key in foreach. The order is
$key => $value, not the reverse.
Summary
- An array holds many values in one variable.
- Indexed arrays use numbers from 0; associative use text keys.
- Loop with
foreach— cleanest for both kinds. - Multidimensional arrays store rows of related data.
- Handy functions:
count,in_array,sort,array_keys.
Next, loop over arrays with PHP loops, or process array text with string functions.
Frequently Asked Questions
What is the difference between an indexed and an associative array?
$a[0]). An associative array uses your own text keys (like $a["name"]). Use indexed for a simple list, and associative when each value needs a meaningful label.Why do PHP arrays start at index 0?
How do I loop through an array?
foreach. For a simple list: foreach ($arr as $value). For key-value pairs: foreach ($arr as $key => $value). foreach handles the counting for you.How do I add an item to an array?
$arr[] = "new" to add to the end, or array_push($arr, "new"). For associative arrays, just assign a key: $arr["city"] = "Delhi".