Reputation:
I have 2 arrays and I would like to delete everything in the first array that is in the second array. In thise case I would like to delete "ibomber" and "apphero" in array one. I tried something with unset, but it doesn't look like it works.
array (size=5)
0 => string 'Air Hippo' (length=9)
1 => string 'iBomber Defense Pacific' (length=23)
3 => string 'AppHero' (length=7)
5 => string 'Pillboxie' (length=9)
6 => string 'Monogram' (length=8)
array (size=2)
0 => string ' iBomber Defense Pacific' (length=24)
1 => string ' AppHero' (length=8)
This is what I tried:
foreach ($_SESSION["appsarray"] as $k=>$v)
{
foreach ($_SESSION["finalapps"] as $k2=>$v2)
{
if ($v == $v2)
{
unset ($_SESSION["appsarray"][$k]);
}
}
}
Session appsarray is my first array and session finalapps is my second array.
Thanks!
Upvotes: 2
Views: 91
Reputation: 7858
function TrimmedStrCaseCmp($str1,$str2)
{
return strcasecmp(trim(str1),trim(str2));
}
$result = array_udiff(values,to_remove_from_values,'TrimmedStrCaseCmp');
http://php.net/manual/en/function.array-udiff.php
Upvotes: 3
Reputation: 4150
You're looking for array_diff i.e.;
$appsarray = array('Air Hippo','iBomber Defense Pacific','AppHero','Pillboxie','Monogram');
$finalapps = array('iBomber Defense Pacific','AppHero');
$result = array_diff($appsarray,$finalapps);
print_r($result);
Will output;
Array ( [0] => Air Hippo [3] => Pillboxie [4] => Monogram )
Upvotes: 2