2 回答
TA贡献1875条经验 获得超3个赞
这将遍历两个数组(它们需要具有相同的键!)并从第二个数组中获取所有属性并将它们写入数组 1 中的对象。
$arr1 = [ /* your objects */ ];
$arr2 = [ /* your objects */ ];
foreach ($arr1 as $index => $obj) {
$vars = get_object_vars($arr2[$index]);
foreach ($vars as $var => $value) {
$obj->$var = $value;
}
}
如果它只是从数组 2 中“上传”你需要的,那就更容易了:
$arr1 = [ /* your objects */ ];
$arr2 = [ /* your objects */ ];
foreach ($arr1 as $index => $obj) {
$obj->upload = $arr2[$index]->upload;
}
TA贡献1829条经验 获得超9个赞
TRY THIS
$array1 = json_decode(json_encode($arr1), true); // convert to array
$array2 = json_decode(json_encode($arr2), true); // convert to array
$arr_merged = array_merge($array1, $array2); // merge both arrays
$arr_merged = json_decode(json_encode($arr_merged)); // to make it back into an object
- 2 回答
- 0 关注
- 130 浏览
添加回答
举报