PHP Remove Array Items
In this page:
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
सभी 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
// 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);
?>
Login to try C/C++/Java/PHP code in the editor
- किसी indexed array पर unset() इस्तेमाल करना और बाकी बचे items के automatically re-index होने की उम्मीद करना -- unset() इसके बजाय numeric keys में एक gap छोड़ देता है।
- array_pop() (अंत से हटाता है) को array_shift() (शुरुआत से हटाता है) के साथ confuse करना -- गलत वाला इस्तेमाल करना गलत item हटा देता है।
- यह भूल जाना कि 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 करता है।
Chapter Quiz — Complete all 13 topics to unlock
0/13 topics done
Complete these topics first: