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

PHP Remove Array Items

जैसे items किसी array में add होते हैं, वैसे ही उन्हें अक्सर वापस निकालने की भी ज़रूरत होती है -- to-do list से एक complete किया गया task हटाना, एक cancelled order delete करना, या किसी array को सिर्फ अभी भी मायने रखने वाली values तक trim करना। PHP items हटाने के लिए कई functions offer करता है, हर एक थोड़ी अलग situation के लिए suited।
Syntax
php
unset($array[key]);         // remove one item
array_pop($array);          // remove the last
array_shift($array);        // remove the first

unset() से एक Specific Item हटाना

unset($arr["key"]) उसकी key या index से exactly एक item हटाता है, इसे array से पूरी तरह delete करते हुए -- लेकिन एक indexed array पर, यह उस index की जगह एक gap छोड़ देता है, क्योंकि बाकी बचे items automatically renumber नहीं होते।

उदाहरण: Removing a Specific Item with unset()

php
<?php
// Declare `$arr` as an array: `["a", "b", "c"]`
$arr = ["a", "b", "c"];
// Remove `$arr[1]`
unset($arr[1]);
// Print a human-readable dump of `$arr`
print_r($arr);
?>

array_pop() से Last Item हटाना

array_pop($arr) किसी array से last item हटाता है और उस removed value को return करता है, original array को सीधे modify करते हुए -- किसी array को stack जैसा treat करने के लिए एक common pattern, जहाँ सबसे recently added item सबसे पहले वापस process होता है।

उदाहरण: Removing the Last Item with array_pop()

php
<?php
// Declare `$stack` as an array: `["a", "b", "c"]`
$stack = ["a", "b", "c"];
// Declare `$last`, set to `array_pop($stack)`
$last = array_pop($stack);
// Print `$last . "\n"` to the output
echo $last . "\n";
// Print a human-readable dump of `$stack`
print_r($stack);
?>

array_shift() से First Item हटाना

array_shift($arr) किसी array के first item को हटाता और return करता है, फिर automatically बाकी बचे हर numeric key को एक-एक करके downward re-index करता है -- एक queue process करने के लिए उपयोगी जहाँ items को उसी order में handle होना चाहिए जिसमें वे add हुए थे।

उदाहरण: Removing the First Item with array_shift()

php
<?php
// Declare `$queue` as an array: `["a", "b", "c"]`
$queue = ["a", "b", "c"];
// Declare `$first`, set to `array_shift($queue)`
$first = array_shift($queue);
// Print `$first . "\n"` to the output
echo $first . "\n";
// Print a human-readable dump of `$queue`
print_r($queue);
?>

array_filter() से एक Condition Match करने वाले Items हटाना

array_filter($arr, $callback) एक बिल्कुल नया array return करता है जिसमें सिर्फ वे items हैं जिनके लिए $callback true return करता है, effectively वह सब filter out (remove) करते हुए जो pass नहीं करता -- यहाँ बाकी removal functions के उलट, यह original array को modify नहीं करता।

उदाहरण: Removing Items That Match a Condition with array_filter()

php
<?php
// Declare `$numbers` as an array: `[1, 2, 3, 4, 5]`
$numbers = [1, 2, 3, 4, 5];
$odd = array_filter($numbers, function ($n) {
    // Return `$n % 2 !== 0` from this function
    return $n % 2 !== 0;
});
// Print a human-readable dump of `$odd`
print_r($odd);
?>

सभी Items हटाना और एक Array खाली करना

एक empty array literal assign करना, $arr = [], instantly हर item clear कर देता है, variable को एक fresh, empty array में reset करते हुए -- एक साथ सब कुछ हटाने का सबसे simple तरीका, जैसे checkout complete होने के बाद एक cart clear करना।

उदाहरण: Removing All Items and Emptying an Array

php
<?php
// Declare `$cart` as an array: `["item1", "item2"]`
$cart = ["item1", "item2"];
// Declare `$cart` as an empty array
$cart = [];
// Print a human-readable dump of `$cart`
print_r($cart);
?>
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. किसी indexed array पर unset() इस्तेमाल करना और बाकी बचे items के automatically re-index होने की उम्मीद करना -- unset() इसके बजाय numeric keys में एक gap छोड़ देता है।
  2. array_pop() (अंत से हटाता है) को array_shift() (शुरुआत से हटाता है) के साथ confuse करना -- गलत वाला इस्तेमाल करना गलत item हटा देता है।
  3. यह भूल जाना कि array_filter() matching items हटाए जाने के बाद एक NEW array return करता है, original array को in place modify करने के बजाय।
चैप्टर सारांश
  • unset($arr["key"]) key या index से एक specific item हटाता है, लेकिन बाद में numeric indexing में एक gap छोड़ सकता है।
  • array_pop($arr) last item हटाता और return करता है; array_shift($arr) first item हटाता और return करता है।
  • array_filter($arr, $callback) सिर्फ एक दी गई test pass करने वाले items वाला एक नया array return करता है, effectively वह सब हटाते हुए जो इसे fail करता है।

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.