'Comparing 2 json arrays by key value using PHP

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



Solution 1:[1]

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

}
?>

Solution 2:[2]

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

}
?>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2