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

报告相同问题?

悬赏问题

  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址
  • ¥15 elmos524.33 eeprom的读写问题
  • ¥15 用ADS设计一款的射频功率放大器
  • ¥15 怎么求交点连线的理论解?