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?
foreach($a3 as $value){
if(isset($a2[$value]))
unset($a2[$value]);
}
Don't get it.
$aNew = $a2;
foreach($a3 as $key) unset($aNew[$key]);
you need this one?
$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: +$
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]);
}
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.
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.
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!!!