猿问

需要创建对象

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 ) )


慕斯王
浏览 131回答 2
2回答

翻过高山走不出你

这将遍历两个数组(它们需要具有相同的键!)并从第二个数组中获取所有属性并将它们写入数组 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;}

PIPIONE

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
随时随地看视频慕课网APP
我要回答