Parse JSON & remove items in PHP

451

So, I have some JSON data that looks like this:

  {  
   "Table1":[  
      {  
         "CURRENCY_FLAG":"EUR",
         "TRADE_DATE":"2015-10-15",
         "DELIVERY_DATE":"2015-10-15",
         "DELIVERY_HOUR":"7",
         "DELIVERY_INTERVAL":"1",
         "RUN_TYPE":"EA",
         "SMP":"35.370",
         "LAMBDA":"35.370",
         "SYSTEM_LOAD":"3164.611",
         "CMS_TIME_STAMP":"2015-10-14T10:03:09+01:00"
      },
      {  
         "CURRENCY_FLAG":"GBP",
         "TRADE_DATE":"2015-10-15",
         "DELIVERY_DATE":"2015-10-15",
         "DELIVERY_HOUR":"7",
         "DELIVERY_INTERVAL":"1",
         "RUN_TYPE":"EA",
         "SMP":"26.460",
         "LAMBDA":"26.460",
         "SYSTEM_LOAD":"3164.611",
         "CMS_TIME_STAMP":"2015-10-14T10:03:09+01:00"
      }... etc

I'm pretty basic at PHP, but have fetched this data with CURL, and now I want to iterate through this data and remove every node with the "GBP" value for "CURRENCY_FLAG" and just hang onto those with the "EUR" sign.

Can anyone point me in the right direction of parsing this with PHP?

Thanks!

916

Answer

Solution:

Try simply this way after decoding your json string usingjson_decode()

//decode json string
$result = json_decode($curl_result, true);

 $final_result = [];
 foreach($result['Table1'] as $key => $value){
    if($value['CURRENCY_FLAG'] != 'GBP'){  //exclude currency flag with GBP
      $final_result[] = $value;
    }
 }

print '<pre>';
print_r($final_result);
print '</pre>';

People are also looking for solutions to the problem: javascript - react.js with laravel relationship

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.