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.
Comment


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