dongxin8392 2019-05-23 07:49
浏览 52

在codeigniter中合并两个std类对象

i have an array of std class $fee_str and an another std class $total_amount. I want to marge both array to get one another std class object.

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;

i want to get results like this

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

        [1] => stdClass Object
            (
                [class] => 10
                [fee_amount] => 300
            )
    )
  • 写回答

1条回答 默认 最新

  • douyuefu1372 2019-05-23 10:11
    关注

    Demo Link.

    Please find inline doc for explanation,

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么