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

PHP Associative Arrays

एक associative array एक dictionary जैसा है जहाँ आप किसी word का meaning ढूँढने के लिए उसे लुकअप करते हैं। numbers के बजाय, आप हर value ढूँढने के लिए age या city जैसे names इस्तेमाल करते हैं।
Syntax
php
$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
<?php
// Declare `$user` as an array: `['email' => '[email protected]']`
$user = ['email' => '[email protected]'];
// Print `$user['email']` to the output
echo $user['email'];
?>

Elements Access और Modify करना

आप एक को [name => Aditi, age => 24] से define करते हैं, जहाँ => हर key को उसकी value से separate करता है, और यह PHP का सबसे नज़दीकी equivalent है जिसे दूसरी languages dictionary, hash map, या object literal कहती हैं।

उदाहरण: Accessing and Modifying Elements

php
<?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'];
?>

नए 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
<?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";
}
?>

Associative Arrays में Loop चलाना

isset($array[key]) यह check करता है कि इसे पढ़ने की कोशिश करने से पहले एक दी गई key exist करती है, optional fields — जैसे एक middle name — array से missing होने पर PHP notices से बचाते हुए।

उदाहरण: Looping Through Associative Arrays

php
<?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.";
}
?>

एक Key Exist करती है या नहीं Check करना

Associative arrays एक single structured record represent करने के लिए natural fit हैं, जैसे किसी database query से एक row, जहाँ हर key एक specific column और उसकी value को नाम देती है।

उदाहरण: Checking If a Key Exists

php
<?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'];
?>
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. एक ऐसी key पढ़ना जो exist नहीं करती, जो एक undefined array key warning raise करता है; isset या ?? से check करें।
  2. $arr[name] जैसे बिना quotes की key लिखना, जिसे एक constant माना जाता है और PHP 8 में fail हो जाता है।
  3. एक array literal में duplicate keys इस्तेमाल करना और दोनों की उम्मीद करना, जबकि last value पहले वाली को overwrite कर देती है।

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.