Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Substract one array from another

Tags:

arrays

php

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?

like image 792
Jeremy Roy Avatar asked Apr 22 '26 17:04

Jeremy Roy


1 Answers

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]);
}
like image 58
oezi Avatar answered Apr 24 '26 06:04

oezi