Types of Arrays in PHP: Indexed, Associative, Multidimensional
Indexed arrays use 0,1,2 keys; associative arrays use named keys like 'roll_no'; multidimensional arrays nest arrays inside arrays. All three with foreach patterns.
Three shelves in one cupboard
1. Indexed array — numbered slots
<?php
$subjects = array("Hindi", "English", "Math"); // PHP 7.2 safe syntax
// $subjects = ["Hindi", "English", "Math"]; // short syntax, also fine
echo $subjects[0]; // Hindi (counting starts at 0)
$subjects[] = "Science"; // append at the end
foreach ($subjects as $i => $sub) {
echo ($i + 1) . ". " . $sub . "\n";
}
?>
2. Associative array — named slots (the DB-row shape)
<?php
$student = array(
"roll_no" => 101,
"name" => "Aman",
"class" => "10",
"fees" => 1500.50
);
echo $student["name"]; // Aman - fetch by label
foreach ($student as $key => $value) {
echo $key . " : " . $value . "\n";
}
?>
Look familiar? This is exactly what PDO's fetch(PDO::FETCH_ASSOC) returns for one database row, and what $_POST gives you from a form. Associative arrays are not a side topic — they ARE everyday PHP.
3. Multidimensional array — the full class register
<?php
$students = array( // array OF associative arrays
array("roll" => 101, "name" => "Aman", "marks" => 92),
array("roll" => 102, "name" => "Priya", "marks" => 88),
array("roll" => 103, "name" => "Rahul", "marks" => 76)
);
echo $students[1]["name"]; // Priya (row 1, column "name")
// The pattern you will write 1000 times (fetchAll gives this exact shape):
foreach ($students as $s) {
echo $s["roll"] . " - " . $s["name"] . " - " . $s["marks"] . "\n";
}
?>
The helper functions you will actually use
<?php
$marks = array(92, 88, 76);
echo count($marks); // 3 - how many elements
echo array_sum($marks); // 256 - total
echo max($marks); // 92 - topper
sort($marks); // ascending: 76, 88, 92
print_r($marks); // debug view of any array
echo in_array(88, $marks) ? "yes" : "no"; // yes - value exists?
echo implode(", ", $marks); // "76, 88, 92" - array -> string
$parts = explode(",", "a,b,c"); // string -> array
?>
| Type | Keys | Real-life shape | Comes from |
|---|---|---|---|
| Indexed | 0, 1, 2... | Simple list | explode(), file() |
| Associative | Named strings | One record | $_POST, fetch() |
| Multidimensional | Mixed nesting | Full table | fetchAll(), JSON |
Interview line: "PHP arrays are ordered maps wearing three shapes — indexed for lists, associative for records, multidimensional for tables — and foreach with $key => $value handles them all."
एक अलमारी की तीन shelves
1. Indexed array — numbered खाने
<?php
$subjects = array("Hindi", "English", "Math"); // PHP 7.2 safe syntax
// $subjects = ["Hindi", "English", "Math"]; // short syntax, यह भी ठीक
echo $subjects[0]; // Hindi (गिनती 0 से)
$subjects[] = "Science"; // आखिर में जोड़ो
foreach ($subjects as $i => $sub) {
echo ($i + 1) . ". " . $sub . "\n";
}
?>
2. Associative array — named खाने (DB-row की shape)
<?php
$student = array(
"roll_no" => 101,
"name" => "Aman",
"class" => "10",
"fees" => 1500.50
);
echo $student["name"]; // Aman - label से निकालो
foreach ($student as $key => $value) {
echo $key . " : " . $value . "\n";
}
?>
जाना-पहचाना लगा? PDO का fetch(PDO::FETCH_ASSOC) एक database row के लिए बिल्कुल यही return करता है, और form से $_POST भी यही देता है. Associative arrays कोई side topic नहीं — यही रोज़ का PHP हैं.
3. Multidimensional array — पूरा class register
<?php
$students = array( // associative arrays KA array
array("roll" => 101, "name" => "Aman", "marks" => 92),
array("roll" => 102, "name" => "Priya", "marks" => 88),
array("roll" => 103, "name" => "Rahul", "marks" => 76)
);
echo $students[1]["name"]; // Priya (row 1, column "name")
// वह pattern जो आप 1000 बार लिखेंगे (fetchAll यही shape देता है):
foreach ($students as $s) {
echo $s["roll"] . " - " . $s["name"] . " - " . $s["marks"] . "\n";
}
?>
Helper functions जो सच में use होंगे
<?php
$marks = array(92, 88, 76);
echo count($marks); // 3 - कितने elements
echo array_sum($marks); // 256 - total
echo max($marks); // 92 - topper
sort($marks); // ascending: 76, 88, 92
print_r($marks); // किसी भी array का debug view
echo in_array(88, $marks) ? "yes" : "no"; // yes - value मौजूद है?
echo implode(", ", $marks); // "76, 88, 92" - array -> string
$parts = explode(",", "a,b,c"); // string -> array
?>
| Type | Keys | असली shape | कहां से आता है |
|---|---|---|---|
| Indexed | 0, 1, 2... | Simple list | explode(), file() |
| Associative | Named strings | एक record | $_POST, fetch() |
| Multidimensional | Mixed nesting | पूरी table | fetchAll(), JSON |
Interview line: "PHP arrays तीन shapes पहनने वाले ordered maps हैं — lists के लिए indexed, records के लिए associative, tables के लिए multidimensional — और $key => $value वाला foreach तीनों संभाल लेता है."
Frequently Asked Questions
What are the types of arrays in PHP?
Indexed arrays with numeric keys 0,1,2 for lists; associative arrays with named string keys for records like a DB row; and multidimensional arrays (arrays inside arrays) for full tables like fetchAll() results.
How do I access a value in a multidimensional array?
Chain the keys left to right: $students[1]["name"] means take row 1 from $students, then take the "name" element of that row.
What does foreach with $key => $value do?
It loops over any array giving both the key and the value each turn — index and item for indexed arrays, column name and cell for associative ones.