我有一个对象数组示例:
更大的套装:
Array(
[0] stdClass Object(
[ID] => 1,
[name] => monkey,
[sub] => help
),
[1] stdClass Object(
[ID] => 1,
[name] => tooth,
[sub] => tip
),
[2] stdClass Object(
[ID] => 1,
[name] => who,
[sub] => knows
),
)
较小的集
Array(
[0] stdClass Object(
[ID] => 1,
[name] => monkey,
[sub] => help
)
期望的结果:
Array(
[0] stdClass Object(
[ID] => 1,
[name] => monkey,
[sub] => help,
[selected] => yes
),
[1] stdClass Object(
[ID] => 1,
[name] => tooth,
[sub] => tip,
[selected] => no
),
[2] stdClass Object(
[ID] => 1,
[name] => who,
[sub] => knows,
[selected] => no
),
)
我试图玩的似乎不起作用的是
foreach($result_static as $stock)
{
foreach($result_memb as $memb_choice)
{
$stock->selected = "false";
//echo $stock->name .' == '. $memb_choice->name.'<br>';
if($stock->name == $memb_choice->name)
{
$stock->selected = "yes";
}
}
$output[] = $stock;
}
这似乎与任何实际结果都不匹配,并且从我收集的内容来看,我的 foreach 逻辑是错误的,因为它重新循环了第二个循环很多次,或者第一个循环以这种方式与另一个循环在一起,每个人的名字永远不会像我希望的那样分别匹配。因此,我正在尝试寻找有关如何更好地解决此问题的想法,并希望一些更优化的东西会很好,但我会采取我目前能得到的。