← Back to PHP Course | Chapter 7: Arrays | Lesson 9 of 13

PHP Multidimensional Arrays

एक multidimensional array drawers की एक chest जैसा है जहाँ हर drawer चीज़ों का अपना छोटा box रखता है। यह आपको tables, जैसे students और उनके grades, एक जगह रखने देता है।
Syntax
php
$array = [
    ["key" => value, "key2" => value2],
    ["key" => value, "key2" => value2],
];

$array[0]["key"];             // read nested value
$array[1]["key2"] = new_value;

Multidimensional Arrays का Introduction

एक multidimensional array बस एक ऐसा array है जिसकी values खुद arrays हैं, आपको grid-like या nested data model करने देते हुए जैसे rows और columns का एक spreadsheet, या एक अपनी properties के set वाले हर user की एक list।

उदाहरण: Introduction to Multidimensional Arrays

php
<?php
$users = [
    ["name" => "Alice", "age" => 30],
    ["name" => "Bob", "age" => 25],
];
// Print a human-readable dump of `$users`
print_r($users);
?>

Nested Elements Access करना

आप index brackets को chain करके एक nested value access करते हैं, जैसे एक 2D grid के लिए $matrix[1][2] या associative arrays की एक list में पहले user के email के लिए $users[0][email]।

उदाहरण: Accessing Nested Elements

php
<?php
// Declare `$matrix` as an array: `[[1, 2], [3, 4]]`
$matrix = [[1, 2], [3, 4]];
// Print `$matrix[1][0] . "\n"` to the output
echo $matrix[1][0] . "\n";

// Declare `$users` as an array: `[["email" => "[email protected]"]]`
$users = [["email" => "[email protected]"]];
// Print `$users[0]['email']` to the output
echo $users[0]['email'];
?>

Multidimensional Arrays Modify करना

एक common real-world shape associative arrays का एक array है — उदाहरण के लिए, database query results आमतौर पर इस तरह return होते हैं, हर outer element एक row represent करते हुए और इसकी inner keys columns represent करते हुए।

उदाहरण: Modifying Multidimensional Arrays

php
<?php
$rows = [
    ["id" => 1, "name" => "Alice"],
    ["id" => 2, "name" => "Bob"],
];
$rows[0]["name"] = "Alicia";
// Print a human-readable dump of `$rows`
print_r($rows);
?>

Multidimensional Arrays में Loop चलाना

Nested foreach loops किसी multidimensional array में चलने का standard तरीका हैं: हर row के लिए एक outer loop और हर column के लिए एक inner loop, जो exactly है कि आप tabular data से एक HTML table कैसे render करेंगे।

उदाहरण: Looping Through Multidimensional Arrays

php
<?php
// Declare `$grid` as an array: `[[1, 2], [3, 4]]`
$grid = [[1, 2], [3, 4]];
// Loop over `$grid`, binding each item to `$row`
foreach ($grid as $row) {
    // Loop over `$row`, binding each item to `$cell`
    foreach ($row as $cell) {
        // Print `$cell . " "` to the output
        echo $cell . " ";
    }
    // Print "\n" to the output
    echo "\n";
}
?>

Multi-level Arrays

print_r() या var_dump() multidimensional arrays debug करने के लिए invaluable हैं, क्योंकि वे पूरा nested structure और types एक नज़र में reveal करते हैं आपको यह guess करने के बजाय कि nesting कितनी गहरी जाती है।

उदाहरण: Multi-level Arrays

php
<?php
// Declare `$data` as an array: `["user" => ["name" => "Alice", "roles" => ["admin", "editor"]]]`
$data = ["user" => ["name" => "Alice", "roles" => ["admin", "editor"]]];
// Print a human-readable dump of `$data`
print_r($data);
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. indexes का order mix up करना, जैसे $matrix[$row][$col] के बजाय $matrix[$col][$row]।
  2. एक single foreach से loop चलाना और inner values तक पहुँचने की उम्मीद करना, जबकि एक nested loop चाहिए।
  3. एक ऐसी inner key access करना जो exist नहीं करती और एक undefined key warning पाना।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.