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

发布于 2025-01-11 14:38:35 字数 1639 浏览 0 评论 0原文

我有 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 中,如果是,则比较“qty”,如果不是,则执行某些操作,反之亦然。

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

<?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

如何解决这个问题?有没有什么功能可以轻松地做到这一点?

提前致谢

I have 2 json arrays in which I want to check if the sku from one array is found in the another, and then check the qty value for each 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"
  }
]

I need to know for each SKU in JSON 1 if it is present in JSON 2, if yes, compare "qty", if not do something and vise versa.

The initial code I used to try to demonstrate the output

<?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
        
    }

}
?>

It works well if there is only 1 sku in the array but once more SKUs are added the qty for other SKUs "$oqty" is 0

how to fix that? and is there any function that can do that in an easy way?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北座城市 2025-01-18 14:38:35

当您满足条件时,您需要中断第二个 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";

}
?>

You need to break the second foreach loop when u satisfy your condition, You can do that by the help of $isExist variable .

like the following


<?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";

}
?>

温暖的光 2025-01-18 14:38:35

您的错误是您正在检查第二次循环后的值。它总是取最后的结果。

<?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)
    ///////////////////////////////////////

}
?>

Your mistake is that you are checking the value after the second loop. It will always take the last result.

<?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)
    ///////////////////////////////////////

}
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文