Site Fuse! programming, design, hosting, promotion and advertising tips for webmasters

PHP Subtract one array from another

Home  \  Forums  \  Programming & Development Forums  \  PHP Forums  \  PHP Subtract one array from another
Share   

user photo

MrPhear   1 years, 5 months ago

For subtracting one array from another in PHP you can use array_diff() function.

So if you subtract array b from a
$a = array('1', '2', 'aaa', '3');
$b = array('3', '1', '2', 'bbb');

print_r(array_diff($a, $b));


You will get

Array('aaa')


and if you subtract a from b

$a = array('1', '2', 'aaa', '3');
$b = array('3', '1', '2', 'bbb');

print_r(array_diff($b, $a));


You will get

Array('bbb')
user photo
Guest