需要创建对象

时间:2019-05-07 08:10:46

标签: php codeigniter

clip

我需要在上方放大两个数组,如下所示。

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

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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