为了账号安全,请及时绑定邮箱和手机立即绑定

需要创建对象

需要创建对象

PHP
慕斯王 2021-06-03 21:17:06
Array ( [0] => stdClass Object ( [download] => 8.63058 [date] => 2019 03 23 12:16 ) [1] => stdClass Object ( [download] => 10.94184 [date] => 2019 03 23 12:17 ) [2] => stdClass Object ( [download] => 11.37850 [date] => 2019 03 23 12:18 ) )  Array ( [0] => stdClass Object ( [upload] => 2.95235 ) [1] => stdClass Object ( [upload] => 2.87732 ) [2] => stdClass Object ( [upload] => 3.58455 ) )我需要上面的两个数组,如下所示。Array ( [0] => stdClass Object ( [download] => 8.63058 [upload]=>2.95235 [date] => 2019 03 23 12:16 ) [1] => stdClass Object ( [download] => 10.94184 [upload]=>2.87722 [date] => 2019 03 23 12:17 ) [2] => stdClass Object ( [download] => 11.37850 [upload]=>3.58455 [date] => 2019 03 23 12:18 ) )
查看完整描述

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;

}


查看完整回答
反对 回复 2021-06-04
?
PIPIONE

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


查看完整回答
反对 回复 2021-06-04
  • 2 回答
  • 0 关注
  • 130 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信