PHP Update Array Items
In this page:
$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
// 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"];
?>
Login to try C/C++/Java/PHP code in the editor
एक साथ कई Items Update करना
foreach से किसी array पर loop चलाना और हर value को reference से modify करना (&$value इस्तेमाल करते हुए) आपको एक single pass में हर item update करने देता है, एक percentage discount जैसा same transformation एक साथ सब पर apply करते हुए।
उदाहरण: Updating Multiple Items at Once
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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"];
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
- गलती से एक बिल्कुल नई key को assign करना (key name में एक typo) intended existing को update करने के बजाय, चुपचाप एक extra, unwanted array entry बनाते हुए।
- original के बजाय किसी array की एक copy update करना, क्योंकि PHP arrays किसी नए variable में assign होने या ज़्यादातर functions को pass होने पर value से copy होते हैं।
- 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 से पहले एक & चाहिए।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: