tags:

views:

1425

answers:

4

I think questions like this are the reason why I don't like working with PHP. The manual is good, if you can find what you are looking for. After reading through the Array Functions, I didn't see one that provides the functionality I need.

I have an array (in my case, numerically indexed) that I want to scan for a particular value and, if it's there, remove it. And then, when all instances of that value have been removed, I want to rearrange the array using array_values.

+5  A: 

array_diff is what you want.

$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red");
$result = array_diff($array1, $array2);

Result: "blue".

Konrad Rudolph
Thanks. I think it would have helped if this was called something like array_prune, array_remove, or something else more descriptive. Thanks again.
Thomas Owens
I agree that the PHP (array) function names are somewhat arbitrary and inconsistent. I've often spent a lot of time searching for the right function myself.
Konrad Rudolph
Especially when I don't open every function and read the details. I didn't even know from "Computes the difference of arrays" that this function returned an array, so I discarded it and kept reading on. I thought it might return an integer diff, like a levenshein (sp?) distance between arrays.
Thomas Owens
Yeah - I thought it'd give a result of all elements which appear in only one array. It appears not.
seanyboy
+2  A: 

Just to add to this...

array_diff appears to show elements in the first array which don't appear in the second array. It doesn't show those elements which only appear in one or the other array.

e.g.

<?
$test = "hello";
$array1 = array("a" => "green", "red", "bicycle", "red");
$array2 = array("b" => "green", "yellow", "red", "blue", "yellow", "pink");
$result = array_diff($array1, $array2);
print_r ($result);
?>

returns

Array
(
    [1] => bicycle
)
seanyboy
A: 

I like the array_diff function, but I have my one scripted one if you dont want to pass down an array:

function array_unset_value($value, &$array) {
    $key = array_search($value, $array);
    while ($key !== false) {
        unset($array[$key]);
        $key = array_search($value, $array);
    }
}
Jrgns
A: 

array_filter does this for you. You just need to provide a filter callback function:

function myFilter($Value){
   if($Value == 'red'){
      return false;
   }
   return true;
}

$Values = array("a" => "green", "red", "bicycle", "red");

$Values = array_filter($Values, 'myFilter');

returns:

array {
  ["a"] => "green"
  [1]   => "bicycle"
}

The filter function should return true for values you want to keep and false for those you wish to remove. Then just go ahead and use array_values to re-index the array. e.g.

$Values = array_values(array_filter($Values, 'myFilter'));

If you are doing this within an object and you want to call a filter method within the object you can use this form for the callback:

array_filter($Values, array($this,'myFilter'));
Jon