数组1:
array (size=1)
0 =>
object(stdClass)[93]
public 'case' => string '12571' (length=5)
public 'status' => string 'aktiv' (length=5)
public 'id' => string '001345821' (length=9)
public 'expires' => string '1392681600' (length=10)
数组2:
array (size=66)
0 =>
object(stdClass)[25]
public 'id' => string '001345821' (length=9)
public 'date' => int 1415602800
public 'amount' => string '1069.31' (length=7)
1 =>
object(stdClass)[26]
public 'id' => string '001345680' (length=9)
public 'date' => int 1415602800
public 'amount' => string '1035.00' (length=7)
我想在" id"上合并数组1和2字段,只保留数组1的匹配。
我想要的是什么:
array ()
0 =>
object(stdClass)
public 'case' => string '1257' (length=5)
public 'status' => string 'aktiv' (length=5)
public 'id' => string '001345821' (length=9)
public 'expires' => string '1392681600' (length=10)
public 'date' => int 1415602800
public 'amount' => string '1069.31' (length=7)
答案 0 :(得分:0)
如果您的对象仅包含属性,则在这种情况下可以使用数组合并。当然,首先,只需使用一个循环。然后在循环下,让你的if
检查那些共享相同ID的人,然后你需要将它们作为数组合并,然后再将它们重新设置为对象:
foreach($array2 as &$value2) {
foreach($array1 as $value1) {
if($value2->id == $value1->id) {
$value2 = (object) array_merge((array) $value2, (array) $value1);
}
}
}