我有这两个数组,它们的 ID [3 和 4] 是常见的,ID [10] 是不同的。我只需要 ID 为 [10] 的商品
<?php
$usergroups = array();
$usergroups[] = array("id" => 3, "name" => "Parish 3");
$usergroups[] = array("id" => 4, "name" => "Parish 4");
$u= (object) $usergroups;
var_dump($u);
echo '<hr />';
$featuredgroups = array();
$featuredgroups [] = array("id" => 3, "name" => "Parish 3");
$featuredgroups [] = array("id" => 4, "name" => "Parish 4");
$featuredgroups [] = array("id" => 10, "name" => "Parish 10");
$fg = (object) $featuredgroups;
var_dump($fg);
$filtered = array_diff($featuredgroups, $usergroups);
var_dump($filtered); //returns empty result
?>
如何过滤掉用户的特色?我试过这个
$filtered = array();
foreach($u as $uu)
{
$ug = (object) $uu;
foreach($fg as $ff)
{
$fgr = (object) $ff;
if($fgr->id != $ug->id)
echo "<br>---".$fgr->name; //am supposed to add this to filtered
}
}
它正在打印出来
---Parish 4
---Parish 10
---Parish 3
---Parish 10
而不仅仅是'Parish 10',这是不同的。
更新
出于某种原因,array_diff
如答案中所建议的那样不起作用。这仅适用于线性/一维数组。我用多维数组尝试过,就像问题中的那个一样,它没有用
$filtered = array_diff($featuredgroups, $usergroups);
var_dump($filtered); //returns empty result