我有一个结构如下:
funActivities(array)
0(array)
Activity(array)
id 4
name walks
1(array)
Activity(array)
id 5
name cycling
2(array)
Activity(array)
id 6
name sand pit
然后另一个像:
activities
0(array)
id 4
name walks
1(array)
id 6
name sand pit
我想比较两个数组并最终得到一个数组,该数组只包含第一个数组中未出现在第二个数组中的活动。所以在这种情况下,我最终只是在阵列中骑自行车。它在第一个数组中,但不在第二个数组中。
最好的办法是什么?
答案 0 :(得分:0)
我制作了一个适合你的PHP功能...
我希望这可以提供帮助。当然有一些事情我可以做得更好,但对于这个szenario它的工作
<?php
$funActivities[0]['Activity']['id'] = 4;
$funActivities[0]['Activity']['name'] = "walks";
$funActivities[1]['Activity']['id'] = 5;
$funActivities[1]['Activity']['name'] = "cycling";
$funActivities[2]['Activity']['id'] = 6;
$funActivities[2]['Activity']['name'] = "sand pit";
$activities[0]['id'] = 4;
$activities[0]['name'] = "walks";
$activities[1]['id'] = 6;
$activities[1]['name'] = "sand pit";
function compareArray($needle,$haystack)
{
$tmpArray = $haystack;
foreach($needle as $n)
{
$s1 = $n['id'].$n['name'];
$count = 0;
foreach($haystack as $h)
{
$s2 = $h['Activity']['id'].$h['Activity']['name'];
if( $s1 == $s2)
{
unset($tmpArray[$count]);
}
$count++;
}
}
return array_values($tmpArray);
}
$arr = compareArray($activities,$funActivities);
echo "<pre>";
print_r($arr);
echo "</pre>";
?>