PHP Associative Arrays
In this page:
$array = ["key1" => value1, "key2" => value2];
$array["key1"]; // read
$array["key3"] = value3; // add
foreach ($array as $key => $value) { }
Associative Arrays क्या हैं?
एक associative array automatic integers के बजाय strings (या कोई भी scalar value) को keys की तरह इस्तेमाल करता है, आपको data को labeled fields की तरह store करने देते हुए — जैसे $user[email] — जो एक numbered index से कहीं ज़्यादा clearly पढ़ता है।
उदाहरण: What are Associative Arrays?
<?php
// Declare `$user` as an array: `['email' => '[email protected]']`
$user = ['email' => '[email protected]'];
// Print `$user['email']` to the output
echo $user['email'];
?>
Login to try C/C++/Java/PHP code in the editor
Elements Access और Modify करना
आप एक को [name => Aditi, age => 24] से define करते हैं, जहाँ => हर key को उसकी value से separate करता है, और यह PHP का सबसे नज़दीकी equivalent है जिसे दूसरी languages dictionary, hash map, या object literal कहती हैं।
उदाहरण: Accessing and Modifying Elements
<?php
// Declare `$person` as an array: `['name' => 'Aditi', 'age' => 24]`
$person = ['name' => 'Aditi', 'age' => 24];
// Print `$person['name'] . " is " . $person['age']` to the output
echo $person['name'] . " is " . $person['age'];
?>
Login to try C/C++/Java/PHP code in the editor
नए Key-Value Pairs Add करना
foreach ($array as $key => $value) आपको एक साथ label और data दोनों पर loop चलाने देता है, जो किसी user की profile जैसे associative array में हर field को print या process करने का standard तरीका है।
उदाहरण: Adding New Key-Value Pairs
<?php
// Declare `$profile` as an array: `['name' => 'Alice', 'age' => 30]`
$profile = ['name' => 'Alice', 'age' => 30];
// Loop over `$profile`, binding each item to `$key => $value`
foreach ($profile as $key => $value) {
// Print "$key: $value\n" to the output
echo "$key: $value\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
Associative Arrays में Loop चलाना
isset($array[key]) यह check करता है कि इसे पढ़ने की कोशिश करने से पहले एक दी गई key exist करती है, optional fields — जैसे एक middle name — array से missing होने पर PHP notices से बचाते हुए।
उदाहरण: Looping Through Associative Arrays
<?php
// Declare `$person` as an array: `['name' => 'Alice']`
$person = ['name' => 'Alice'];
// Check whether `isset($person['middleName'])`
if (isset($person['middleName'])) {
// Print `$person['middleName']` to the output
echo $person['middleName'];
// Otherwise, run this branch
} else {
// Print "No middle name set." to the output
echo "No middle name set.";
}
?>
Login to try C/C++/Java/PHP code in the editor
एक Key Exist करती है या नहीं Check करना
Associative arrays एक single structured record represent करने के लिए natural fit हैं, जैसे किसी database query से एक row, जहाँ हर key एक specific column और उसकी value को नाम देती है।
उदाहरण: Checking If a Key Exists
<?php
// Declare `$row` as an array: `['id' => 1, 'name' => 'Alice', 'email' => '[email protected]']`
$row = ['id' => 1, 'name' => 'Alice', 'email' => '[email protected]'];
// Print `$row['name']` to the output
echo $row['name'];
?>
Login to try C/C++/Java/PHP code in the editor
- एक ऐसी key पढ़ना जो exist नहीं करती, जो एक undefined array key warning raise करता है;
issetया??से check करें। $arr[name]जैसे बिना quotes की key लिखना, जिसे एक constant माना जाता है और PHP 8 में fail हो जाता है।- एक array literal में duplicate keys इस्तेमाल करना और दोनों की उम्मीद करना, जबकि last value पहले वाली को overwrite कर देती है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: