PHP Multidimensional Arrays
In this page:
$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
$users = [
["name" => "Alice", "age" => 30],
["name" => "Bob", "age" => 25],
];
// Print a human-readable dump of `$users`
print_r($users);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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'];
?>
Login to try C/C++/Java/PHP code in the editor
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
$rows = [
["id" => 1, "name" => "Alice"],
["id" => 2, "name" => "Bob"],
];
$rows[0]["name"] = "Alicia";
// Print a human-readable dump of `$rows`
print_r($rows);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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";
}
?>
Login to try C/C++/Java/PHP code in the editor
Multi-level Arrays
print_r() या var_dump() multidimensional arrays debug करने के लिए invaluable हैं, क्योंकि वे पूरा nested structure और types एक नज़र में reveal करते हैं आपको यह guess करने के बजाय कि nesting कितनी गहरी जाती है।
उदाहरण: Multi-level Arrays
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
- indexes का order mix up करना, जैसे
$matrix[$row][$col]के बजाय$matrix[$col][$row]। - एक single
foreachसे loop चलाना और inner values तक पहुँचने की उम्मीद करना, जबकि एक nested loop चाहिए। - एक ऐसी inner key access करना जो exist नहीं करती और एक undefined key warning पाना।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: