RError.com

RError.com Logo RError.com Logo

RError.com Navigation

  • 主页

Mobile menu

Close
  • 主页
  • 系统&网络
    • 热门问题
    • 最新问题
    • 标签
  • Ubuntu
    • 热门问题
    • 最新问题
    • 标签
  • 帮助
主页 / 问题 / 557880
Accepted
Егор Rmd
Егор Rmd
Asked:2020-08-22 07:34:38 +0000 UTC2020-08-22 07:34:38 +0000 UTC 2020-08-22 07:34:38 +0000 UTC

如何显示json字符串的差异?

  • 772

有2个json { "orderID": 773, "shopperName": "Трегубенко Егор", "shopperEmail": "sinecura92@mail.ru", "contents": [ { "productID": 34, "productName": "Холодильник, indesit 423jd2", "quantity": 1 }, { "productID": 56, "productName": "Наушники, Philips", "quantity": 3 } ], "orderCompleted": true }

因此**第二** { "orderID": 773, "shopperName": "Трегубенко Егор", "shopperEmail": "sinecura92@mail.ru", "contents": [ { "productID": 31, "productName": "Стиральная машина bosch", "quantity": 2 }, { "productID": 56, "productName": "Наушники, Philips", "quantity": 3 } ], "orderCompleted": true }

问:如何只显示差异?尝试使用 array_dif 但我得到一个空数组

ps 表示 php

php
  • 2 2 个回答
  • 10 Views

2 个回答

  • Voted
  1. Best Answer
    Stanislav
    2020-08-22T10:44:23Z2020-08-22T10:44:23Z

    手动检查并输出:

    $a = '{
      "orderID": 773,
      "shopperName": "Трегубенко Егор",
      "shopperEmail": "sinecura92@mail.ru",
      "contents": [
        {
          "productID": 34,
          "productName": "Холодильник, indesit 423jd2",
          "quantity": 1
        },
        {
          "productID": 56,
          "productName": "Наушники, Philips",
          "quantity": 3
        }
      ],
      "orderCompleted": true
    }';
    $b = '{
      "orderID": 773,
      "shopperName": "Трегубенко Егор",
      "shopperEmail": "sinecura92@mail.ru",
      "contents": [
        {
          "productID": 31,
          "productName": "Стиральная машина bosch",
          "quantity": 2
        },
        {
          "productID": 56,
          "productName": "Наушники, Philips",
          "quantity": 3
        }
      ],
      "orderCompleted": true
    }';
    $arr1 = json_decode($a, true);
    $arr2 = json_decode($b, true);
    
    $result = my_array_compare($arr1, $arr2, 0);
    
    function my_array_compare($arr1, $arr2, $level) {
        $result = [];
    
        foreach ($arr2 as $key => $value) {
            if (!isset($arr1[$key])) {
                $result[$key] = ['not exist', $value];
            }
            else if ($arr1[$key]!=$value) {
                if (is_array($arr2[$key])) {
                    if ($level<1) {
                        $result[$key] = my_array_compare($arr1[$key], $arr2[$key], $level + 1);
                    }
                    else {
                        $result[$key] = [$arr1[$key], $value];
                    }
                }
                else {
                    $result[$key] = [$arr1[$key], $value];
                }
            }
        }
        return $result;
    }
    
    print_r($result);
    
    $result = compare_contents($arr1['contents'], $arr2['contents']);
    
    function compare_contents($arr1, $arr2) {
        $cArr1 = $cArr2 = $result = [];
        foreach ($arr1 as $item) {
            $cArr1[$item['productID']] = $item['quantity'];
        }
        foreach ($arr2 as $item) {
            $cArr2[$item['productID']] = $item['quantity'];
        }
    
        foreach ($cArr1 as $key => $value) {
            if (!isset($cArr2[$key])) {
                $result['_left'][$key] = [$value, 'not exist'];
            }
            else if ($value!=$cArr2[$key]) {
                $result['_left'][$key] = [$value, $cArr2[$key]];
            }
        }
    
        foreach ($cArr2 as $key => $value) {
            if (!isset($cArr1[$key])) {
                $result['_right'][$key] = ['not exist', $value];
            }
            else if ($value!=$cArr1[$key]) {
                $result['_right'][$key] = [$cArr1[$key], $value];
            }
        }
    
        return $result;
    }
    
    print_r($result);
    

    我写了 2 个函数: 1 - 按现有顺序直接比较数组元素;2 - 通过'contents'数组的现有值进行比较(因为这个数组中元素的顺序和数量可能不同)。

    功能结果my_array_compare:

    Array
    (
        [contents] => Array
            (
                [0] => Array
                    (
                        [0] => Array
                            (
                                [productID] => 34
                                [productName] => Холодильник, indesit 423jd2
                                [quantity] => 1
                            )
    
                        [1] => Array
                            (
                                [productID] => 31
                                [productName] => Стиральная машина bosch
                                [quantity] => 2
                            )
    
                    )
    
            )
    
    )
    

    功能结果compare_contents:

    Array
    (
        [_left] => Array
            (
                [34] => Array
                    (
                        [0] => 1
                        [1] => not exist
                    )
    
            )
    
        [_right] => Array
            (
                [31] => Array
                    (
                        [0] => not exist
                        [1] => 2
                    )
    
            )
    
    )
    

    在函数中,my_array_compare我提供了嵌套级别的比较。

    也许不是最优雅的例子,而是一个有效的例子。

    • 2
  2. Jarry Roxwell
    2020-08-22T10:01:02Z2020-08-22T10:01:02Z

    这可能不是最简洁的解决方案,但它有效:

        function Fr($aM, $bM, $preKey){
            foreach ($aM as $key => $value){
                foreach ($bM as $key2 => $value2){
                    if($key==$key2){
                        if(gettype($value) != "array"){
    
                            if($value2!=$value){
                                $buff.="<br>$preKey|$key|=>$value :: $preKey|$key2|=>$value2<br>";
                                break;
                            }else{
                                break;
                            }
                        }else{
                            $buff.=Fr($aM[$key], $bM[$key], $preKey."|".$key);
                            break;
                        }
                    }
                }   
    
            }
        return $buff;
        }   
        $a='{
          "orderID": 773,
          "shopperName": "Трегубенко Егор",
          "shopperEmail": "sinecura92@mail.ru",
          "contents": [
            {
              "productID": 34,
              "productName": "Холодильник, indesit 423jd2",
              "quantity": 1
            },
            {
              "productID": 56,
              "productName": "Наушники, Philips",
              "quantity": 3
            }
          ],
          "orderCompleted": true
        }';
        $b='{
          "orderID": 773,
          "shopperName": "Трегубенко Егор",
          "shopperEmail": "sinecura92@mail.ru",
          "contents": [
            {
              "productID": 31,
              "productName": "Стиральная машина bosch",
              "quantity": 2
            },
            {
              "productID": 56,
              "productName": "Наушники, Philips",
              "quantity": 3
            }
          ],
          "orderCompleted": true
        }';
        $aM=json_decode($a, true);
        $bM=json_decode($b, true);   
        echo "<pre>".Fr($aM, $bM, "")."</pre>";
    

    输出:

    |contents|0|productID|=>34 :: |contents|0|productID|=>31
    
    |contents|0|productName|=>Холодильник, indesit 423jd2 ::|contents|0|productName|=>Стиральная машина bosch
    
    |contents|0|quantity|=>1 :: |contents|0|quantity|=>2
    
    • 1

相关问题

Sidebar

Stats

  • 问题 10021
  • Answers 30001
  • 最佳答案 8000
  • 用户 6900
  • 常问
  • 回答
  • Marko Smith

    如何停止编写糟糕的代码?

    • 3 个回答
  • Marko Smith

    onCreateView 方法重构

    • 1 个回答
  • Marko Smith

    通用还是非通用

    • 2 个回答
  • Marko Smith

    如何访问 jQuery 中的列

    • 1 个回答
  • Marko Smith

    *.tga 文件的组重命名(3620 个)

    • 1 个回答
  • Marko Smith

    内存分配列表C#

    • 1 个回答
  • Marko Smith

    常规赛适度贪婪

    • 1 个回答
  • Marko Smith

    如何制作自己的自动完成/自动更正?

    • 1 个回答
  • Marko Smith

    选择斐波那契数列

    • 2 个回答
  • Marko Smith

    所有 API 版本中的通用权限代码

    • 2 个回答
  • Martin Hope
    jfs *(星号)和 ** 双星号在 Python 中是什么意思? 2020-11-23 05:07:40 +0000 UTC
  • Martin Hope
    hwak 哪个孩子调用了父母的静态方法?还是不可能完成的任务? 2020-11-18 16:30:55 +0000 UTC
  • Martin Hope
    Qwertiy 并变成3个无穷大 2020-11-06 07:15:57 +0000 UTC
  • Martin Hope
    koks_rs 什么是样板代码? 2020-10-27 15:43:19 +0000 UTC
  • Martin Hope
    user207618 Codegolf——组合选择算法的实现 2020-10-23 18:46:29 +0000 UTC
  • Martin Hope
    Sirop4ik 向 git 提交发布的正确方法是什么? 2020-10-05 00:02:00 +0000 UTC
  • Martin Hope
    Arch ArrayList 与 LinkedList 的区别? 2020-09-20 02:42:49 +0000 UTC
  • Martin Hope
    iluxa1810 哪个更正确使用:if () 或 try-catch? 2020-08-23 18:56:13 +0000 UTC
  • Martin Hope
    faoxis 为什么在这么多示例中函数都称为 foo? 2020-08-15 04:42:49 +0000 UTC
  • Martin Hope
    Pavel Mayorov 如何从事件或回调函数中返回值?或者至少等他们完成。 2020-08-11 16:49:28 +0000 UTC

热门标签

javascript python java php c# c++ html android jquery mysql

Explore

  • 主页
  • 问题
    • 热门问题
    • 最新问题
  • 标签
  • 帮助

Footer

RError.com

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

帮助

© 2023 RError.com All Rights Reserve   沪ICP备12040472号-5