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

PHP Access Array Items

किसी array में values store करना सिर्फ तभी उपयोगी है जब आप उन्हें दोबारा retrieve कर सकें -- एक array item access करने का मतलब है किसी specific position (indexed arrays के लिए) या किसी specific key (associative arrays के लिए) के तहत stored value पढ़ना, PHP का square bracket syntax इस्तेमाल करके।
Syntax
php
$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
<?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];
?>

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
<?php
// Declare `$person` as an array: `["name" => "Alice", "age" => 30]`
$person = ["name" => "Alice", "age" => 30];
// Print `$person["name"]` to the output
echo $person["name"];
?>

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
<?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));
?>

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
<?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";
?>

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
<?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"];
?>
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. एक ऐसी index या key access करना जो exist नहीं करती, जो एक "undefined array key" warning trigger करती है और एक real value के बजाय null return करती है।
  2. यह भूल जाना कि indexed array positions 0 से शुरू होती हैं, 1 से नहीं -- पहला element $arr[0] है, $arr[1] नहीं।
  3. "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 पढ़ता है।

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.