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

PHP Add Array Items

एक array को अक्सर पहली बार बनने के बाद बढ़ने की ज़रूरत होती है -- एक cart में एक नया item add होता है, एक नई comment post होती है, एक query से एक नया record वापस आता है। PHP आपको किसी array में items add करने के कई तरीके देता है: अंत में append करना, शुरुआत में insert करना, या एक बिल्कुल नई associative key set करना।
Syntax
php
$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
<?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);
?>

array_push() से कई Items Add करना

array_push($arr, $value1, $value2, ...) एक ही call में एक या ज़्यादा values को किसी array के अंत में append करता है, जो तब उपयोगी है जब आपके पास पहले से कई नई values एक साथ add करने के लिए तैयार हों, उन्हें एक-एक करके append करने के बजाय।

उदाहरण: Adding Multiple Items with array_push()

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

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

एक नई 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
<?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);
?>

दो 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
<?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));
?>
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. एक item "add" करने के लिए $arr[0] = $value इस्तेमाल करना, जो असल में अगर पहले से exist करता है तो index 0 को overwrite कर देता है, एक genuinely नया item append करने के बजाय।
  2. यह भूल जाना कि array_push() और array_unshift() बाद में सभी numeric keys को re-index कर देते हैं, जो उन indexes को shift कर सकता है जिन पर आप कहीं और भरोसा कर रहे थे।
  3. किसी 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 नहीं करती, यह बना दी जाती है।

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.