在codeigniter中合并两个std类对象

时间:2019-05-23 07:49:41

标签: php codeigniter-3

我有一个std类$ fee_str和另一个std类$ total_amount数组。我想同时插入两个数组以获得另一个std类对象。

Array
    (
        [0] => stdClass Object
            (
                [class] => 1
            )

        [1] => stdClass Object
            (
                [class] => 10
            )
    )

    Array
    (
        [0] => stdClass Object
            (
                [fee_amount] => 8285
            )

        [1] => stdClass Object
            (
                [fee_amount] => 300
            )
    )

    $fee_str=$this->fee_settings_model->load_fee_structure();

            $i=0;
                foreach ($fee_str as $key => $classes) {
                    $total_amount[$i]=$this->fee_settings_model->get_total_by_class($classes->class);
                    $i++;
                }
                $data=array_merge($fee_str,$total_amount);
                echo "<pre>";
                print_r($data);
                exit;

我想得到这样的结果

Array
    (
        [0] => stdClass Object
            (
                [class] => 1
                [fee_amount] => 8285
            )

        [1] => stdClass Object
            (
                [class] => 10
                [fee_amount] => 300
            )
    )

1 个答案:

答案 0 :(得分:0)

Demo Link

请找到内联文档进行解释,

function multiArrayCombine($array1, $array2)
{
    $temp = array_map(null, $array1, $array2); // switch index wise data on either side
    foreach ($temp as $key => &$value) { // & to make changes at address of variable
        $value = json_decode(json_encode($value), true); // convert object to array
        $value = array_merge(...$value); // removing initial index and merge internally
    }
    return json_decode(json_encode($temp)); // convert back to object form
}
$arr = multiArrayCombine($arr, $arr1);
print_r($arr);