3

Well, I have

$a2 = array('a'=>'Apple','b'=>'bat','c'=>'Cat','d'=>'Dog','e'=>'Eagle','f'=>'Fox','g'=>'God');
$a3 = array('b','e');

I want to substract $a3 from $a2 to get:

$aNew = array('a'=>'Apple','c'=>'Cat','d'=>'Dog','f'=>'Fox','g'=>'God');

Any help?

0

7 Answers 7

3

the are two built-in functions to do something similar: array_diff and array_diff_assoc - but both won't work in your case.

so, to do what you want, you'll have to change the markup of your $a3 a bit to fit these functions (take a look at the documentation), or you'll have to loop $a3 and delete the elements from $a2 manually like this:

foreach($a3 as $k){
    unset($a2[$k]);
}
Sign up to request clarification or add additional context in comments.

Comments

2
foreach($a3 as $value){

   if(isset($a2[$value]))
         unset($a2[$value]);
}

4 Comments

It's a better one, like the array_key_exists check.
This check is unnesesary and it will be slower
And, anyway, isset better than array_key_exists
foreach($a3 as $value) isset($a2[$value]) && unset($a2[$value]); but, as i said before, it's absolutely unnecessary.
1

Don't get it.

$aNew = $a2;
foreach($a3 as $key) unset($aNew[$key]);

you need this one?

1 Comment

Thanks, thought there might be some inbuilt function of php
1

This can be done with php built-in functions.

Since the array $a2 contains the values of the keys you want to remove, you need first to create an array containing the values of $a2 as keys using array_flip. Then you can just use array_diff_key. So try this:

$aNew = array_diff_key($a2, array_flip($a3));

Note that you need php > 5.1.0 for this to work.

2 Comments

yeah? I thought the built-in functions where really fast. Should not matter if you only have a few elements though.
Just test it on my local comp, just to be sure - 100k iterations with flip is about 0.56sec and with foreach 0.46sec
0
$aNew = array();
foreach (array_diff(array_keys($a2), $a3) : $key) {
   $aNew[key] = $a2[key];
}

This sould do the job. Grap the keys from a2 remove all keys which exist in a3 and then copy the remaining keys to a new array. You could also remove the keys from the old array. This is up to you.

Edit: +$

Comments

0

The difficulty here lies in that you compare key of one array with values of another one. E.g. by the use of array_diff_ukey. Excerpt from PHP manual:

  function key_compare_func($key1, $key2)
  {
     if ($key1 == $key2)
       return 0;
     else if ($key1 > $key2)
       return 1;
    else
       return -1;

}

$array1 = array('blue' => 1, 'red' => 2, 'green' => 3, 'purple' => 4); $array2 = array('green' => 5, 'blue' => 6, 'yellow' => 7, 'cyan' => 8);

var_dump(array_diff_ukey($array1, $array2, 'key_compare_func'));

array(2) { ["red"]=> int(2) ["purple"]=> int(4) }

Your second array is not associative, so this would compare "a" with 0, e.g. So it's:

$compareFunc = function($key) uses ($a3) {
 if(array_key_exists($key, $a3) {
    return true;
 }
 else return false;
}

array_map($compareFunc, $a2);

can't be used neither, because this function is used to modify but not to reduce the array.

$a4 = array();
$compareFunc = function($key) uses ($a3, &$a4) {
 if(array_key_exists($key, $a3) {
    array_push($a4);
 }
} 

would do the job.
What I am trying to achieve is: Play around with the PHP - functions that provide callbacks to you when the standart - functions don't succeed. Try using the concept of binding, when the describtion of the callback doesn't fit your needs. With PHP's Lambda - functions you can bind objects / variables to a function, so that you have access to variables that are not passed to the callback. Get creative!!!

Comments

-2

Try using array_diff but you need to make some changes on your $a3.

$a3 = array('bat', 'Eagle');
//and then
$a4 = array_diff($a2, $3);

because without specifying a key (in key => value format), the string will automatically become a value instead.

1 Comment

and if you cant change input arrays (coming from outside for e.g.)? Not a good idea

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.