【问题标题】:How to compare 2 json to find new entries with depth?如何比较 2 json 以找到具有深度的新条目?
【发布时间】:2023-03-19 01:40:01
【问题描述】:

我想比较2个json,如果有新数组,它将获取新数组并将其插入数据库。

这是旧的json,

[{"menu_id":"1","menu_name":"Rapat 2018","parent_id":"0","link":"informent.online","status":"1"},
{"menu_id":"3","menu_name":"Rapat 2019","parent_id":"0","link":"#","status":"1"}]

这是新的 json,

[{"menu_id":"1","menu_name":"Rapat 2018","parent_id":"0","link":"informent.online","status":"1","children":[{"menu_id":"","menu_name":"Rapat RW","parent_id":"1","link":"#","status":"1"}]},
{"menu_id":"3","menu_name":"Rapat 2019","parent_id":"0","link":"#","status":"1"}]

有来自子项的新数组(menu_name : Rapat RW),我想获取那个新数组,以便将其插入数据库。

这就是我所做的,

$json_str1 = '  [
                      {
                        "menu_id": "1",
                        "menu_name": "Rapat 2018",
                        "parent_id": "0",
                        "link": "informent.online",
                        "status": "1"
                      },
                      {
                        "menu_id": "3",
                        "menu_name": "Rapat 2019",
                        "parent_id": "0",
                        "link": "#",
                        "status": "1"
                      }
                    ]';
    $json_str2 = '  [
                      {
                        "menu_id": "1",
                        "menu_name": "Rapat 2018",
                        "parent_id": "0",
                        "link": "informent.online",
                        "status": "1",
                        "children": [
                          {
                            "menu_id": "",
                            "menu_name": "Rapat RW",
                            "parent_id": "1",
                            "link": "#",
                            "status": "1"
                          }
                        ]
                      },
                      {
                        "menu_id": "3",
                        "menu_name": "Rapat 2019",
                        "parent_id": "0",
                        "link": "#",
                        "status": "1"
                      }
                    ]';

    list($arr1, $arr2) = [json_decode($json_str1), json_decode($json_str2)];
    $common_items = array_intersect($arr2, $arr1);
    $result = array_filter(array_merge($arr1, $arr2), function($v) use($common_items){
        return !in_array($v, $common_items);
    });

    print_r($result);

但我从“函数:array_intersect”返回错误“stdClass 类的对象无法转换为字符串” 问题是什么? 任何帮助将不胜感激!

【问题讨论】:

  • 请发布您迄今为止尝试过的内容以及对此无效的内容。 Stackoverflow 不是代码编写服务,而是帮助您解决具体问题的平台。请参考:stackoverflow.com/help/how-to-ask
  • 解码json,然后检查children是否存在?如果存在,则仅比较新旧数据的子项,并基于该获取新数组,您最终必须将其插入到数据库中
  • [json_decode($json_str1), json_decode($json_str2)] 必须是 [json_decode($json_str1,true), json_decode($json_str2,true)]
  • 现在它返回“数组到字符串的转换”
  • 这确实发生在其他地方,它在你的代码中看不到数组到字符串,你有更多相关的代码吗?

标签: php arrays json compare


【解决方案1】:

我看到 array_intersect 将值作为字符串进行比较。因此数组到字符串错误。因为你的价值观是一个数组本身。要解决这个问题,您必须使用array_uintersect

http://php.net/array_uintersect

function myFunction($value1, $value2) {
    //compare and return 1 or 0
    return (int) $value1 !== $value2; //for example
}
$common_items = array_uintersect($arr2, $arr1, "myFunction");

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-18
    • 1970-01-01
    • 2015-10-19
    • 2021-11-29
    • 2012-02-13
    • 2017-05-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多