Free tutorials & notes in Hindi & English · Clean code examples 🧰 मुफ़्त PDF और फ़ोटो टूल्स →
Blog · PHP · 04 Jul 2026 · Hindi + English

PHP में Arrays के प्रकार: Indexed, Associative, Multidimensional

Indexed arrays 0,1,2 keys use करते हैं; associative arrays 'roll_no' जैसी named keys; multidimensional arrays के अंदर arrays. तीनों 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

PHP में arrays के प्रकार कौन-से हैं?

Lists के लिए 0,1,2 numeric keys वाले indexed arrays; DB row जैसे records के लिए named string keys वाले associative arrays; और fetchAll() results जैसी पूरी tables के लिए multidimensional arrays (arrays के अंदर arrays).

Multidimensional array की value कैसे access करें?

Keys को बाएं से दाएं जोड़ें: $students[1]["name"] मतलब $students से row 1 लो, फिर उस row का "name" element.

$key => $value वाला foreach क्या करता है?

यह किसी भी array पर घूमते हुए हर बार key और value दोनों देता है — indexed arrays में index और item, associative में column का नाम और cell.

यह lesson कैसा लगा?

आपकी राय से हम content और बेहतर बनाते हैं।

अपना review और सुझाव लिखें (वैकल्पिक)

Review approve होने के बाद आपका नाम और शहर इसके साथ public दिख सकते हैं। Email या phone न लिखें।

▶ CodingEasily on YouTube

Video से सीखें – आसान Hindi में

चुनी हुई video: RDBMS भाग 3 – Class XI Commerce IT

▶ Channel subscribe करें