Element Deletion from Array in PHP

PHP have a large number of native functions for array operations. When I am coding in javascript, I envy this feature of PHP. Many people have gone to the extend of porting the most needed functions over to javascript. However, you will find that one function is missing in PHP - the remove function for an array element.

Basically, I want to remove an element from an array - there is no function for that. So I wrote these functions to solve this problem...


<?php

////////////////////////////////// Lists(Numerical Arrays) /////////////////////////////////
/**
 * A small function to remove an element from a list(numerical array)
 * Arguments:    $arr - The array that should be edited
 *                $value - The value that should be deleted.
 * Returns    : The edited array
 */
function array_remove($arr,$value) {
   return 
array_values(array_diff($arr,array($value)));
}

////////////////////////////////// Associative Arrays //////////////////////////////////////
/**
 * This function will remove all the specified keys from an array and return the final array.
 * Arguments :    The first argument is the array that should be edited
 *                The arguments after the first argument is a list of keys that must be removed.
 * Example : array_remove_key($arr,"one","two","three");
 * Return : The function will return an array after deleting the said keys
 */
function array_remove_key() {
    
$args func_get_args();
    
$arr $args[0];
    
$keys array_slice($args,1);
    
    foreach(
$arr as $k=>$v) {
        if(
in_array($k$keys))
            unset(
$arr[$k]);
    }
    return 
$arr;
}

/**
 * This function will remove all the specified values from an array and return the final array.
 * Arguments :    The first argument is the array that should be edited
 *                The arguments after the first argument is a list of values that must be removed.
 * Example : array_remove_value($arr,"one","two","three");
 * Return : The function will return an array after deleting the said values
 */
function array_remove_value() {
    
$args func_get_args();
    
$arr $args[0];
    
$values array_slice($args,1);
    
    foreach(
$arr as $k=>$v) {
        if(
in_array($v$values))
            unset(
$arr[$k]);
    }
    return 
$arr;
}

Comments

Anonymous at 22 May, 2007 07:09
Thankyou very much for posting this you GENIUS....
Reply to this.
ASCII at 10 Jul, 2007 08:55
Thank you for posting these!
Reply to this.
Andy at 12 Nov, 2007 11:54
Thank you so much
Reply to this.
Anonymous at 29 Feb, 2008 07:40
merci pour votre poste. c'est très nourrissant je trouve ;). est-ce possible de vous en proposer aussi ? salut.
Reply to this.
Brad at 15 Mar, 2008 01:46
Thanks a ton you just saved me lot's of time and I like that!
Reply to this.
Anonymou at 26 Mar, 2008 05:49
Thanks
Reply to this.
Geo varghese at 07 Apr, 2008 09:12
Nice script ..Its really helpful.....Lokking for other useful scripts from you..
Reply to this.
google pagerank at 07 Apr, 2008 09:16
I forgot to mention that..I liked 3 functions..really great..helpful for php programers..
Reply to this.
Anonymous at 20 May, 2008 02:45
Nice!
Reply to this.
fustaki at 13 Aug, 2008 01:41
Nice, thank you!
Reply to this.
Jay at 20 Aug, 2008 03:59
There is a problem in this function, when you delete a value from an array.. if the array holds more than one similar values, this function will delete them as well. To put it simply, it removes all the values which matches the case, but I think it should be limited. This function would work perfectly IF the arrays are deleted by the indexes... like array_remove($arr, 2);
Reply to this.
Cody at 21 Aug, 2008 02:25
I agree with Jay. Is there a way for elements to be deleted by the index position instead? That, for me at least, would prove much more useful.
Reply to this.
Chuck at 04 Sep, 2008 08:00
You could use good ol' unset() I guess, e.g.

$numbers = array(1,2,3,4,5);
unset($numbers[3]); // removes "4"

it keeps the same indexes, but if you want to reorder them you could additionally run it through the array_values() function, something like this:

$numbers = array_values($numbers);

this will reindex them
Reply to this.
Anonymous at 11 Sep, 2008 04:44
Worked perfectly for my purposes, thank you very much.
Reply to this.
Matt at 26 Feb, 2009 03:16
Thanks for the very useful codes. Great!
Reply to this.
Bill Brown at 26 Mar, 2009 01:47
You actually don't even need any loops for this:


function array_remove_key ()
{
$args = func_get_args();
return array_diff_key($args[0],array_flip(array_slice($args,1)));
}
function array_remove_value ()
{
$args = func_get_args();
return array_diff($args[0],array_slice($args,1));
}

$my_array = array(
'apples' => 5,
'bananas' => 105,
'blueberries' => 105,
'oranges' => 23,
'pears' => 47,
'strawberries' => 47
);

echo '<pre>Original array: ',print_r($my_array,TRUE),'</pre>';

$my_array = array_remove_key($my_array,"pears","bananas");
echo '<pre>Remove "pear" and "bananas" key: ',print_r($my_array,TRUE),'</pre>';

$my_array = array_remove_value($my_array,105,47);
echo '<pre>Remove values 105 and 47: ',print_r($my_array,TRUE),'</pre>';

Reply to this.
Anonymous at 24 Aug, 2009 10:57
That was really a very helpful one! Thanks for this.
Reply to this.
Anonymous at 03 Feb, 2010 12:06
thank you
Reply to this.
Anonymous at 05 Feb, 2010 11:42
hey man its confusing.
can any body tel how to implement all this.
where to write the function where to call it from.....
i'm totally messed up with this.....
Reply to this.
Anonymous at 15 Feb, 2010 05:16
Yes You are a Genius!!!! Thanks!
Reply to this.
Comment

Please dont enter you comments in this form - this is a fake form to confuse spamming bots. The next form is the real one.




Comment




Comment Formating : HTML tags a, strong, em, b, i, code, pre, p and br allowed. Other tags will be shown as code(< will become &lt;). Urls, Line breaks will be auto-formated.
Subscribe to Feed