PHP Add Array Items
In this page:
$array[] = $value; // append
array_push($array, $value1, $value2); // append several
array_unshift($array, $value); // add at the start
[] Syntax से Append करना
$arr[] = $value किसी indexed array में एक single नया item append करने का सबसे simple और सबसे common तरीका है -- PHP automatically अगली available numeric index पता लगाता है और वहाँ value assign कर देता है, आपको खुद current length track करने की ज़रूरत के बिना।
उदाहरण: Appending with the [] Syntax
<?php
// Declare `$fruits` as an array: `["Apple", "Banana"]`
$fruits = ["Apple", "Banana"];
// Append "Cherry" to the end of `$fruits`
$fruits[] = "Cherry";
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>
Login to try C/C++/Java/PHP code in the editor
array_push() से कई Items Add करना
array_push($arr, $value1, $value2, ...) एक ही call में एक या ज़्यादा values को किसी array के अंत में append करता है, जो तब उपयोगी है जब आपके पास पहले से कई नई values एक साथ add करने के लिए तैयार हों, उन्हें एक-एक करके append करने के बजाय।
उदाहरण: Adding Multiple Items with array_push()
<?php
// Declare `$fruits` as an array: `["Apple"]`
$fruits = ["Apple"];
// Call `array_push($fruits, "Banana", "Cherry")`
array_push($fruits, "Banana", "Cherry");
// Print a human-readable dump of `$fruits`
print_r($fruits);
?>
Login to try C/C++/Java/PHP code in the editor
array_unshift() से शुरुआत में Items Add करना
array_unshift($arr, $value1, $value2, ...) एक या ज़्यादा values को किसी array की बिल्कुल शुरुआत में insert करता है, हर मौजूदा item को एक ऊँची index पर shift करते हुए -- array_push() से विपरीत छोर, किसी list के front में एक "most recent" item add करने के लिए उपयोगी।
उदाहरण: Adding Items at the Beginning with array_unshift()
<?php
// Declare `$queue` as an array: `["Bob", "Carol"]`
$queue = ["Bob", "Carol"];
// Call `array_unshift($queue, "Alice")`
array_unshift($queue, "Alice");
// Print a human-readable dump of `$queue`
print_r($queue);
?>
Login to try C/C++/Java/PHP code in the editor
एक नई Associative Key Add करना
किसी associative array में एक नई key add करना उतना ही simple है जितना एक ऐसी key को value assign करना जो अभी exist नहीं करती: $person["email"] = "[email protected]" "email" key बनाता है अगर यह पहले से मौजूद नहीं थी, बिल्कुल एक normal assignment जैसा।
उदाहरण: Adding a New Associative Key
<?php
// Declare `$person` as an array: `["name" => "Alice"]`
$person = ["name" => "Alice"];
// Set `$person["email"]` to "[email protected]"
$person["email"] = "[email protected]";
// Print a human-readable dump of `$person`
print_r($person);
?>
Login to try C/C++/Java/PHP code in the editor
दो Arrays को Merge करना
array_merge($arr1, $arr2) दो arrays को एक नए array में combine करता है, effectively दूसरे array से हर item को पहले पर add करते हुए -- indexed arrays के लिए यह keys को sequentially फिर से number करता है, जबकि matching keys वाली associative arrays के लिए, बाद वाले array की values जीतती हैं।
उदाहरण: Merging Two Arrays Together
<?php
// Declare `$arr1` as an array: `["a", "b"]`
$arr1 = ["a", "b"];
// Declare `$arr2` as an array: `["c", "d"]`
$arr2 = ["c", "d"];
// Print a human-readable dump of `array_merge($arr1, $arr2)`
print_r(array_merge($arr1, $arr2));
?>
Login to try C/C++/Java/PHP code in the editor
- एक item "add" करने के लिए $arr[0] = $value इस्तेमाल करना, जो असल में अगर पहले से exist करता है तो index 0 को overwrite कर देता है, एक genuinely नया item append करने के बजाय।
- यह भूल जाना कि array_push() और array_unshift() बाद में सभी numeric keys को re-index कर देते हैं, जो उन indexes को shift कर सकता है जिन पर आप कहीं और भरोसा कर रहे थे।
- किसी loop के अंदर array_push($arr, $item) से items add करना बजाय simpler $arr[] = $item के, जो single-item appends के लिए छोटा और थोड़ा तेज़ दोनों है।
- $arr[] = $value किसी indexed array के अंत में एक नया item append करता है, automatically इसे अगली numeric index assign करते हुए।
- array_push($arr, ...$values) अंत में एक या ज़्यादा values append करता है; array_unshift($arr, ...$values) उन्हें शुरुआत में insert करता है।
- एक नई associative key add करना उतना ही simple है जितना $arr["newKey"] = $value -- अगर key पहले से exist नहीं करती, यह बना दी जाती है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: