Re: Array remove function?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Unset works, but if he's trying to do a search and remove in one function, unset is a step removed from that criteria (missing the 'search' part).

Looking at the array functions, I see some potential.

# array_remove(array(1=>2,2=>3),2,true); // array (2=>3)
// Keep all but where values = 2
$new_array = array_diff(array(1=>2, 2=>3, 3=>4), array(2));
print_r($new_array);

# array_remove(array(1=>2,2=>3),2,false); // array (1=>2)
// Keeps only where values = 2
$new_array = array_intersect(array(1=>2, 2=>3, 3=>4), array(2));
print_r($new_array);


So keeping the same syntax you had:

function array_remove($array, $remove, $remove_value = true) {

  $remove = array($remove);

  if ($remove_value) {
    return array_diff($array, $remove);
  } else {
    return array_intersect($array, $remove);
  }

}

Not a single function but a simpler version of your function.

-TG

= = = Original message = = =

http://php.net/unset


On Tue, April 10, 2007 2:49 pm, Tijnema ! wrote:
> Hi,
>
> Is there currently a function that removes a key/value from an array?
> I use this code right now:
> function array_remove($array,$remove,$remove_value = true)
> 
> ~foreach($array as $key => $value) 
> ~~if($remove_value && $value != $remove) 
> ~~~$new_array[$key] = $value;
> ~~ elseif (!$remove_value && $key != $remove) 
> ~~~$new_array[$key] = $value;
> ~~
> ~
> ~return $new_array;
> 
>
> array_remove(array(1=>2,2=>3),2,true); // array (2=>3)
> array_remove(array(1=>2,2=>3),2,false); // array (1=>2)
>
> Anyone knows if there already exists such function?
>
> Else should i create future request?
>
> Tijnema
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>


-- 
Some people have a "gift" link here.
Know what I want?
I want you to buy a CD from some indie artist.
http://cdbaby.com/browse/from/lynch
Yeah, I get a buck. So?

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



___________________________________________________________
Sent by ePrompter, the premier email notification software.
Free download at http://www.ePrompter.com.

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux