使用 PHP 按键值比较 2 json 数组

Comparing 2 json arrays by key value using PHP

我有 2 个 json 数组,我想在其中检查一个数组中的 sku 是否在另一个数组中找到,然后检查每个 sku 的数量值

JSON 1

 [
   {
     "sku": "888",
     "qty": "6.00",
     "price": "100"
   },
   {
     "sku": "999",
     "qty": 1,
     "price": "40"
   },
   {
    "sku": "555",
    "qty": "2.00",
    "price": "50"
   }
 ]

JSON 2

[
  {
    "sku": "888",
    "qty": "6.00",
    "price": "100"
  },
  {
    "sku": "999",
    "qty": "2.00",
    "price": "40"
  },
  {
    "sku": "444",
    "qty": "2.00",
    "price": "45"
  }
]

我需要知道 JSON 1 中的每个 SKU 是否存在于 JSON 2 中,如果存在,比较“数量”,如果不存在则做某事,反之亦然。

我用来演示输出的初始代码

<?php
$original_sale_details = '[{"sku":"888","qty":"6.00","price":"100"},{"sku":"999","qty":1,"price":"40"},{"sku":"555","qty":"2.00","price":"50"}]';
$values = json_decode($original_sale_details, true);

$new_sale2 = '[{"sku":"888","qty":"6.00","price":"100"},{"sku":"999","qty":"2.00","price":"40"},{"sku":"444","qty":"2.00","price":"45"}]';
$new_sale = json_decode($new_sale2, true);

foreach ($values as $value)
{

    foreach ($new_sale as $ovalue)
    {

        if ($value['sku'] == $ovalue['sku'])
        {
            $oqty = $ovalue['qty'];
        }
        else
        {
            $oqty = 0;
        }
    }

    if ($value['qty'] == $oqty)
    {

        //do smth
        
    }
    elseif ($value['qty'] > $oqty)
    {
        //do smth
        
    }
    elseif ($value['qty'] < $oqty)
    {
        //do smth
        
    }

}
?>

如果数组中只有 1 个 sku,则效果很好,但一旦添加更多 sku,其他 sku“$oqty”的数量为 0

如何解决?有没有什么函数可以简单地做到这一点?

提前致谢

你的错误是你在第二次循环之后检查值。它总是取最后一个结果。

<?php
$original_sale_details = '[{"sku":"888","qty":"6.00","price":"100"},{"sku":"999","qty":1,"price":"40"},{"sku":"555","qty":"2.00","price":"50"}]';
$values = json_decode($original_sale_details, true);

$new_sale2 = '[{"sku":"888","qty":"6.00","price":"100"},{"sku":"999","qty":"2.00","price":"40"},{"sku":"444","qty":"2.00","price":"45"}]';
$new_sale = json_decode($new_sale2, true);

foreach ($values as $value)
{

    foreach ($new_sale as $ovalue)
    {

        if ($value['sku'] == $ovalue['sku'])
        {
            $oqty = $ovalue['qty'];
        }
        else
        {
            $oqty = null;
        }

       /////////////////////////////////
       //section move here
       /////////////////////////////////
        if(is_null($oqty)) {
            echo "----------> QTY is null... Ignore this or remove echo or do something?<br>\n";
        }
        else {
           if ($value['qty'] == $oqty)
           {

               //do smth
               echo "QTY is equal...<br>\n";
            
           }
           elseif ($value['qty'] > $oqty)
           {
               //do smth
               echo "QTY is more...<br>\n";
            
           }
           elseif ($value['qty'] < $oqty)
           {
               //do smth
               echo "QTY is less...<br>\n";
            
           }
        }

       /////////////////////////////////
       //end section move here
       /////////////////////////////////

    }

    ///////////////////////////////////////
    // your code was here (moving code)
    ///////////////////////////////////////

}
?>

当你满足你的条件时,你需要打破第二个 foreach 循环,你可以通过 $isExist 变量的帮助来做到这一点。

点赞


<?php
$original_sale_details = '[{"sku":"888","qty":"6.00","price":"100"},{"sku":"999","qty":"1","price":"40"},{"sku":"555","qty":"2.00","price":"50"}]';
$values = json_decode($original_sale_details, true);

$new_sale2 = '[{"sku":"888","qty":"6.00","price":"100"},{"sku":"999","qty":"2.00","price":"40"},{"sku":"444","qty":"2.00","price":"45"}]';
$new_sale = json_decode($new_sale2, true);

foreach ($values as $value)
{
    $isExist = false;
    foreach ($new_sale as $ovalue)
    {
      
      // if exist
      if ($value['sku'] == $ovalue['sku'])
      {
        // if equal
        if($ovalue['qty'] == $value['qty']){
            echo $value['sku']." is equal <br/>";
            $isExist = true;
            break;
        // if greater than
        }else if($ovalue['qty'] > $value['qty']){
            echo $value['sku']." is greater <br/>";
            $isExist = true;
            break;
        // if less than
        }else{
            echo $value['sku']." is less <br/>";
            $isExist = true;
            break;
        }
      // if not exist     
      }
      
    }
  
  if(!$isExist) echo $value['sku']." does not exist";

}
?>