douwudie8060 2017-11-06 16:27
浏览 110
已采纳

基于属性合并PHP中的对象

What I am originally trying to do is merge two objects

I have one object like this

{#23 ▼
  +"Name": "Consultation"
  +"ServiceId": "1024"
  +"Price": ".00"
}

Another object

{#362 ▼
  +"StartTime": "9:15AM"
  +"ServiceId": "1024"
}

I am looking for a way to merge these two objects based on it's ServiceId, so I get a single object like this

   {#1 ▼
      +"Name": "Consultation"
      +"ServiceId": "1024"
      +"Price": ".00"
      +"StartTime": "9:15AM"
     }

Of course, if the ServiceId between the two objects do not match, it should not merge.

Any ideas on how to solve the error?

  • 写回答

2条回答 默认 最新

  • dougezhua0017 2017-11-06 16:36
    关注

    There seems to be two parts to your question. The first part is how to merge the output of two objects returned by json_decode. The second part is how to merge them only if the ServiceId match.


    Part 1

    json_decode produces, by default, objects of class \StdClass. If you want merge two \StdClass objects into a third \StdClass object, you need some judicious casting:

    $a = json_decode('{"Name":"Consultation", "ServiceId":"1024", "Price":".00"}');
    $b = json_decode('{"StartTime": "9:15AM", "ServiceId": "1024"}');
    $c = (object)array_merge((array)$a, (array)$b);
    var_dump($c);
    

    Here, $a and $b are \StdClass objects. Cast them to array, merge, then cast them back to \StdClass.

    That's a bit of a round-about way to go. So, you can benefit from working with these as arrays from the get-go. json_decode takes an optional second argument, which instructs it to return an array:

    $a = json_decode('{"Name":"Consultation", "ServiceId":"1024", "Price":".00"}', true);
    $b = json_decode('{"StartTime": "9:15AM", "ServiceId": "1024"}', true);
    $c = array_merge($a, $b);
    var_dump($c);
    

    This works in arrays the whole time. If you later want $c to be a \StdClass object, you can cast it using $c = (object)$c; as was done in the first example.

    See these live on 3v4l.org.


    Part 2

    Presumably, you're going to need some logic that iterates or otherwise pairs these objects together. You don't mention the source, but the logic will look something like this:

    if ($a->ServiceId == $b->ServiceId) {
        $c = (object)array_merge((array)$a, (array)$b)
    }
    

    If you have a list of objects, and you want to merge them all together, you can use the combinatoric array walk behavior of usort:

    $merged = [];
    usort($objects, function ($a, $b) use ($merged) {
        $comp = strcmp($a->ServiceId, $b->ServiceId);
        if (0 === $comp) {
            $merged[] = (object)array_merge((array)$a, (array)$b)
        }
        return $comp;
    });
    var_dump($merged);
    

    This iterates through your list of objects, comparing each. If the ServiceId match, then it merges the two objects and adds it to a list of merged. This will happen for as many objects that share ServiceId as you have in your list.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码