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

PHP Update Array Items

एक array की values शायद ही कभी हमेशा के लिए fixed रहती हैं -- एक shopping cart quantity बदलती है, एक user अपनी profile update करता है, एक score बढ़ता है। किसी array item को update करने का मतलब है एक मौजूदा position या key में एक नई value assign करना, वहाँ पहले जो भी store था उसे overwrite करते हुए, इसे पढ़ने के लिए इस्तेमाल होने वाले same square bracket syntax से।
Syntax
php
$array[index_or_key] = new_value;
$array["outer"]["inner"] = new_value;   // nested

foreach ($array as &$value) {
    $value = new_value;
}

Key या Index से एक Single Item Update करना

किसी मौजूदा key या index को एक नई value assign करना वहाँ stored कोई भी value overwrite कर देता है -- $inventory["apples"] = 50 पिछली apple count को 50 से replace करता है, एक नई key बनाने जैसे ही exact syntax इस्तेमाल करते हुए।

उदाहरण: Updating a Single Item by Key or Index

php
<?php
// Declare `$inventory` as an array: `["apples" => 20]`
$inventory = ["apples" => 20];
// Set `$inventory["apples"]` to `50`
$inventory["apples"] = 50;
// Print `$inventory["apples"]` to the output
echo $inventory["apples"];
?>

एक साथ कई Items Update करना

foreach से किसी array पर loop चलाना और हर value को reference से modify करना (&$value इस्तेमाल करते हुए) आपको एक single pass में हर item update करने देता है, एक percentage discount जैसा same transformation एक साथ सब पर apply करते हुए।

उदाहरण: Updating Multiple Items at Once

php
<?php
// Declare `$prices` as an array: `[100, 200, 300]`
$prices = [100, 200, 300];
// Loop over `$prices`, binding each item to `&$price`
foreach ($prices as &$price) {
    $price *= 0.9;
}
// Remove `$price`
unset($price);
// Print a human-readable dump of `$prices`
print_r($prices);
?>

Nested Array Values Update करना

exact nested path तक square brackets chain करना आपको एक deeply nested value सीधे update करने देता है -- $data["user"]["address"]["city"] = "Austin" structure में बाकी किसी चीज़ को बिना disturb किए सिर्फ उस एक nested field को update करता है।

उदाहरण: Updating Nested Array Values

php
<?php
// Declare `$data` as an array: `["user" => ["address" => ["city" => "Boston"]]]`
$data = ["user" => ["address" => ["city" => "Boston"]]];
$data["user"]["address"]["city"] = "Austin";
// Print `$data["user"]["address"]["city"]` to the output
echo $data["user"]["address"]["city"];
?>

Update करते समय Copy-by-Value Behavior

जब आप किसी array को एक नए variable में assign करते हैं, PHP पूरे array को value से copy कर देता है -- copy को update करना original को पूरी तरह untouched छोड़ देता है।

यह objects से अलग है, जिन्हें automatically reference से handle किया जाता है, और अक्सर दूसरी languages से आने वाले developers को surprise करता है।

उदाहरण: Copy-by-Value Behavior When Updating

php
<?php
// Declare `$original` as an array: `[1, 2, 3]`
$original = [1, 2, 3];
// Declare `$copy`, set to `$original`
$copy = $original;
// Set `$copy[0]` to `99`
$copy[0] = 99;
// Print a human-readable dump of `$original`
print_r($original);
// Print a human-readable dump of `$copy`
print_r($copy);
?>

Array Items को Conditionally Update करना

एक if condition को array update syntax के साथ combine करना आपको एक value सिर्फ तभी बदलने देता है जब कोई certain rule satisfy हो, जैसे किसी threshold से नीचे हर price को एक minimum तक बढ़ाना, या overdue items को अलग तरह से mark करना -- records के एक पूरे array को process करते समय एक common pattern।

उदाहरण: Conditionally Updating Array Items

php
<?php
// Declare `$prices` as an array: `[5, 15, 8, 20]`
$prices = [5, 15, 8, 20];
// Loop over `$prices`, binding each item to `&$price`
foreach ($prices as &$price) {
    // Check whether `$price < 10`
    if ($price < 10) {
        // Declare `$price`, set to `10`
        $price = 10;
    }
}
// Remove `$price`
unset($price);
// Print a human-readable dump of `$prices`
print_r($prices);
?>
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 को assign करना (key name में एक typo) intended existing को update करने के बजाय, चुपचाप एक extra, unwanted array entry बनाते हुए।
  2. original के बजाय किसी array की एक copy update करना, क्योंकि PHP arrays किसी नए variable में assign होने या ज़्यादातर functions को pass होने पर value से copy होते हैं।
  3. foreach से किसी array पर loop चलाना और loop variable में किए गए changes के original array को update करने की उम्मीद करना, बिना एक reference (&) इस्तेमाल किए।
चैप्टर सारांश
  • किसी item को update करना इसे पढ़ने जैसा ही same square bracket syntax इस्तेमाल करता है: $arr["key"] = $newValue।
  • PHP arrays default रूप से value से copy होते हैं, इसलिए एक copy update करना original को affect नहीं करता जब तक आप explicitly एक reference इस्तेमाल न करें।
  • loop के दौरान original array के items को actually modify करने के लिए foreach को अपनी value variable से पहले एक & चाहिए।

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.