Free tutorials in Hindi & English Daily computer, mobile and IT guides Beginner friendly learning
Blog · PHP · 04 Jul 2026 · Hindi + English

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

PHP has ONE array type wearing three costumes. Indexed array = a numbered rack (slot 0, 1, 2...). Associative array = a labelled rack ("roll_no" slot, "name" slot). Multidimensional array = racks inside racks — a cupboard where each shelf holds another labelled box. Master the three and 80% of practical PHP (form data, DB rows, JSON) is in your hands.

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";
}
?>
Hindi 1. Hindi 2. English 3. Math 4. Science

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";
}
?>
Aman roll_no : 101 name : Aman class : 10 fees : 1500.5

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";
}
?>
Priya 101 - Aman - 92 102 - Priya - 88 103 - Rahul - 76
Read $students[1]["name"] from left to right: "from $students, take row 1; from that row, take 'name'." Every table you ever print in HTML — fee lists, attendance, results — is exactly this foreach over a multidimensional array from fetchAll(). Nothing more.

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
?>
TypeKeysReal-life shapeComes from
Indexed0, 1, 2...Simple listexplode(), file()
AssociativeNamed stringsOne record$_POST, fetch()
MultidimensionalMixed nestingFull tablefetchAll(), 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

PHP में EK array type है जो तीन वेश धरता है. Indexed array = numbered rack (खाना 0, 1, 2...). Associative array = labelled rack ("roll_no" खाना, "name" खाना). Multidimensional array = racks के अंदर racks — ऐसी अलमारी जिसकी हर shelf में एक और labelled डिब्बा. तीनों पर पकड़ बनी तो practical PHP का 80% (form data, DB rows, JSON) आपके हाथ में.

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";
}
?>
Hindi 1. Hindi 2. English 3. Math 4. Science

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";
}
?>
Aman roll_no : 101 name : Aman class : 10 fees : 1500.5

जाना-पहचाना लगा? 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";
}
?>
Priya 101 - Aman - 92 102 - Priya - 88 103 - Rahul - 76
$students[1]["name"] को बाएं से दाएं पढ़िए: "$students में से row 1 लो; उस row में से 'name' लो." HTML में छापी जाने वाली हर table — fee lists, attendance, results — fetchAll() से आए multidimensional array पर बिल्कुल यही foreach है. बस इतना ही.

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
?>
TypeKeysअसली shapeकहां से आता है
Indexed0, 1, 2...Simple listexplode(), file()
AssociativeNamed stringsएक record$_POST, fetch()
MultidimensionalMixed nestingपूरी tablefetchAll(), 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.