PHP Access Array Items
In this page:
$array[0]; // indexed: first item
$array["key"]; // associative: by key
isset($array["key"]); // exists and not null
array_key_exists("key", $array); // exists
Indexed Array Items Access करना
एक indexed array के items उनकी numeric position से access किए जाते हैं, पहले element के लिए 0 से शुरू होते हुए -- $fruits[0] पहला item लेता है, $fruits[1] दूसरा, वगैरह।
एक indexed array में last valid index हमेशा उसके total item count से एक कम होता है, क्योंकि counting एक के बजाय zero से शुरू होती है।
उदाहरण: Accessing Indexed Array Items
<?php
// Declare `$fruits` as an array: `["Apple", "Banana", "Cherry"]`
$fruits = ["Apple", "Banana", "Cherry"];
// Print `$fruits[0] . "\n"` to the output
echo $fruits[0] . "\n";
// Print `$fruits[2]` to the output
echo $fruits[2];
?>
Login to try C/C++/Java/PHP code in the editor
Associative Array Items Access करना
एक associative array के items एक numeric position के बजाय उनकी string key से access होते हैं -- $person["name"] "name" key के तहत stored value retrieve करता है, चाहे वह key array में कहीं भी बैठी हो।
उदाहरण: Accessing Associative Array Items
<?php
// Declare `$person` as an array: `["name" => "Alice", "age" => 30]`
$person = ["name" => "Alice", "age" => 30];
// Print `$person["name"]` to the output
echo $person["name"];
?>
Login to try C/C++/Java/PHP code in the editor
Access करने से पहले Key Exist करती है या नहीं Check करना
isset($arr["key"]) सिर्फ तभी true return करता है जब key exist करे AND उसकी value null न हो, जबकि array_key_exists("key", $arr) true return करता है अगर key बिल्कुल exist करे, भले ही उसकी value null हो -- पहले check करना उस warning से बचाता है जो genuinely missing key access करने से आती है।
उदाहरण: Checking If a Key Exists Before Accessing It
<?php
// Declare `$arr` as an array: `["name" => "Alice", "email" => null]`
$arr = ["name" => "Alice", "email" => null];
// Print a detailed dump (with types) of `isset($arr["email"])`
var_dump(isset($arr["email"]));
// Print a detailed dump (with types) of `array_key_exists("email", $arr)`
var_dump(array_key_exists("email", $arr));
?>
Login to try C/C++/Java/PHP code in the editor
Null Coalescing Operator के साथ Safe Access
$arr["key"] ?? $default अगर key exist करती है और null नहीं है तो एक value पढ़ता है, नहीं तो $default return करता है -- सब एक expression में, बिना अलग isset() check की ज़रूरत के या undefined-key warning trigger किए।
उदाहरण: Safe Access with the Null Coalescing Operator
<?php
// Declare `$arr` as an array: `["name" => "Alice"]`
$arr = ["name" => "Alice"];
// Print `$arr["email"] ?? "no email set"` to the output
echo $arr["email"] ?? "no email set";
?>
Login to try C/C++/Java/PHP code in the editor
Nested Array Items Access करना
जब कोई array दूसरे arrays रखता है (एक multidimensional array), एक deeply nested value तक पहुँचने के लिए कई square brackets chain करें -- $data["user"]["address"]["city"] final value तक पहुँचने के लिए nested arrays की तीन levels में चलता है।
उदाहरण: Accessing Nested Array Items
<?php
// Declare `$data` as an array: `["user" => ["address" => ["city" => "Austin"]]]`
$data = ["user" => ["address" => ["city" => "Austin"]]];
// Print `$data["user"]["address"]["city"]` to the output
echo $data["user"]["address"]["city"];
?>
Login to try C/C++/Java/PHP code in the editor
- एक ऐसी index या key access करना जो exist नहीं करती, जो एक "undefined array key" warning trigger करती है और एक real value के बजाय null return करती है।
- यह भूल जाना कि indexed array positions 0 से शुरू होती हैं, 1 से नहीं -- पहला element $arr[0] है, $arr[1] नहीं।
- "5" जैसी एक numeric string key और एक true integer key 5 को interchangeably इस्तेमाल करना यह realize किए बिना कि PHP numeric string keys को internally integers की तरह treat करता है।
- Square bracket syntax, $arr[index] या $arr["key"], उस position या key पर stored value पढ़ता है।
- किसी nonexistent key को access करना null return करता है और एक warning raise करता है; isset() या array_key_exists() पहले safely check कर सकते हैं।
- Null coalescing operator, $arr["key"] ?? $default, एक शायद-missing key को एक line में एक fallback के साथ safely पढ़ता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: