How to Remove a Particular Element from an Array in PHP

Other Blogs

Blogs ❯❯ DSA

Image could not load

DSA In Hindi by Sam Pak on Unsplash

जब हम arrays के साथ काम करते हैं, तो कभी कभी हमें particular element को remove करना पड़ता है। ये element किसी भी position पर हो सकता है, या फिर हमें value-based removal करनी होती है।

इन scenarios में हम manual methods use करते हैं, जिसमे predefined functions (जैसे array_splice(), unset(), etc.) का use नहीं होता।

इस blog में हम PHP में array से particular element remove करने के दो different approaches देखेंगे :

  • Index-based removal

  • Value-based removal

हम manually यह कैसे कर सकते हैं, बिना predefined functions के? चलिए, step-by-step समझते हैं !

हम सभी examples PHP में लेने वाले हैं , हालाँकि बाकी language में अगर आप practice करेंगे तो concept same ही रहेगा ,बस syntax change हो जायगा।

Remove element from and array by index in PHP

Index-based removal का मतलब है कि हम directly एक particular index (position) पर जो element है, उससे remove करेंगे। हम manually array को re-arrange करेंगे ताकि वो element remove हो जाये और array का structure intact रहे।

<?php // Function to remove an element by index from an array function removeByIndex($array, $index) { // Initialize a new array to store the result $newArray = []; // Loop through the array for ($i = 0; $i < count($array); $i++) { // Skip the element if it is the one to be removed if ($i != $index) { $newArray[] = $array[$i]; } } return $newArray; } // Example usage: $array = [10, 20, 30, 40, 50]; $index = 2; // We want to remove the element at index 2 (which is 30) $updatedArray = removeByIndex($array, $index); echo "Array after removing element at index $index: "; print_r($updatedArray); ?>

Explanation:

  1. हमने removeByIndex() function बनाया जो array और index लेता है।

  2. हम for loop के through array को traverse करते हैं और अगर current index वो नहीं है जो remove करना है, तो उस element को नया array में add करते हैं। 

  3. इस process में हम manually array को adjust करते हैं बिना किसी predefined function के। 

ऊपर दिए गए example में new array create किया गया है अगर बिना new array create करके same array से remove करना हो तो कुछ ऐसे कर सकते हैं।

// Function to remove an element by index from the same array function removeByIndexInPlace($array, $index) { // Loop through the array starting from the index after the removed element for ($i = $index; $i < count($array) - 1; $i++) { $array[$i] = $array[$i + 1]; // Shift elements left } // Remove the last element of the array since it's duplicated after shifting array_pop($array); return $array; }

हम array को loop करते हैं और जो element index के बाद आता है, उससे एक position shift कर देते हैं। array_pop() से हम last element remove करते हैं, जो अब shifting के बाद redundant हो जाता है।

Remove element from an array in PHP

Value-based removal का मतलब है कि हम directly array में से किसी specific value को remove करेंगे, चाहे वो किसी भी index पर हो।

यहाँ भी हम manual method use करेंगे, जिसमे हम array को traverse करेंगे और जो value match करेगी उससे remove करेंगे।

<?php // Function to remove an element by value from an array function removeByValue($array, $value) { // Initialize a new array to store the result $newArray = []; // Loop through the array foreach ($array as $element) { // Only add elements that are not equal to the value we want to remove if ($element != $value) { $newArray[] = $element; } } return $newArray; } // Example usage: $array = [10, 20, 30, 40, 50]; $value = 30; // We want to remove the value 30 from the array $updatedArray = removeByValue($array, $value); echo "Array after removing the value $value: "; print_r($updatedArray); ?>

Explanation:

  1. हमने removeByValue() function बनाया जो array और value लेता है।

  2. हम foreach loop से array को traverse करते हैं , अगर element match नहीं करता with थे value तो be removed, तो उससे नया array में add करते हैं। 

  3. इस method में हम value को remove करते हैं बिना index के, बस value match hone पर। 

Key Points To Remember

  • Index-based Removal: इस method में हम directly index का use करते हैं। अगर आपको specific position पर element remove करना हो तो यह approach best है।

  • Value-based Removal : इस method में हम किसी value को target करते हैं और उस value को array से remove करते हैं, regardless of its position . 

  • Manual Methods: इन दोनो methods में हम predefined functions का use नहीं कर रहे हैं। सब कुछ manually manage किया जा रहा है।

Conclusion

तो आज हमने सीखा कि कैसे PHP में हम किसी particular element को remove कर सकते हैं बिना predefined functions का use किये।

हमने index-based removal और value-based removal दोनो methods को manually implement किया है। यह approaches especially useful हैं जब आपको custom array manipulation करना हो बिना built-in functions के।

Hey ! I'm Rahul founder of learnhindituts.com. Working in IT industry more than 5.5 years. I love to talk about programming as well as writing technical tutorials and blogs that can help to others .... keep learning :)

Get connected with me - LinkedIn Twitter Instagram Facebook

Your Thought ?

Please wait . . .

    Recent Blogs

    Loading ...

    0 Comment(s) found !