Muyi
Muyi

Reputation: 21

JSON to Php issue

I am trying to echo the following JSON response to html using php.

This is the JSON response :

{
  "data": {
    "onward": [
      {
        "origin": "LHR",
        "id": "SB15",
        "rating": 0,
        "destination": "FKK",
        "PricingSolution": {
          "TotalPrice": "USD445"
        },
        "Class": "Eco"
      },
      {
        "origin": "LHR",
        "id": "EH10",
        "rating": 0,
        "destination": "FKK",
        "PricingSolution": {
          "TotalPrice": "USD223"
        },
        "Class": "Eco"
      }
    ]
  }
}

This is how it should appear in html :

<body>
<ul class="myclass">
<li>ID: EH10, Price: 223, Class: Eco</li>
<li>ID: SB15, Price: 445, Class: Eco</li>
</ul>
</body>

I want it somehow to be sorted by Total price in Ascending order.

Tried

foreach($json['data'] as $data). 

Doesn't seem to be working! Please help.

Upvotes: 1

Views: 88

Answers (2)

Aman Rawat
Aman Rawat

Reputation: 2625

I have executed the code and it's working great.

Check this

<?php

$jsonString = '{
"data": {
"onward": [
{
"origin": "LHR",
"id": "SB15",
"rating": 0,
"destination": "FKK",
"PricingSolution":{
"TotalPrice": "USD442"
},
"Class": "Eco"
},
{
"origin": "LHR",
"id": "SB15",
"rating": 0,
"destination": "FKK",
"PricingSolution":{
"TotalPrice": "USD445"
},
"Class": "Eco"
},

{
"origin": "LHR",
"id": "EH10",
"rating": 0,
"destination": "FKK",
"PricingSolution":{
"TotalPrice": "USD223"
},
"Class": "Eco"
}
]
}
}';

$json = json_decode($jsonString, true);

$items = $json['data']['onward'];

usort($items, function ($a, $b) {
    return (extractPrice($a['PricingSolution']['TotalPrice']) - extractPrice($b['PricingSolution']['TotalPrice']));
});

function extractPrice($price)
{
    return str_replace('USD', '', $price);
}

$finalItems = []; // Duplicate ids handling
foreach ($items as $item) {
    if (empty($finalItems[$item['id']]) || extractPrice($finalItems[$item['id']]['PricingSolution']['TotalPrice']) > extractPrice($item['PricingSolution']['TotalPrice'])) {
        $finalItems[$item['id']] = $item;
    }
}

?>
<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
<ul class="myclass">
    <?php foreach ($finalItems as $item) { ?>
        <li>ID: <?= $item['id']; ?>, Price: <?= $item['PricingSolution']['TotalPrice']; ?>,
            Class: <?= $item['Class']; ?></li>
    <?php } ?>
</ul>
</body>
</html>

If you want to do sorting then check this link

How can I sort arrays and data in PHP?

Upvotes: 1

Louis Loudog Trottier
Louis Loudog Trottier

Reputation: 1377

use json_decode to transform your json into a php array (with the 2nd parameter as true so it is cast as an associative array)

PHP manual for json_decode

Then you can loop the array.

$array=json_decode($json,true);
foreach($array as $key=>$value){
  //do your magic
}

I've ran this fiddle

<?php
$json=json_decode ('{
"data": {
"onward": [
{
"origin": "LHR",
"id": "SB15",
"rating": 0,
"destination": "FKK",
"PricingSolution":{
"TotalPrice": "USD445"
},
"Class": "Eco"
},

{
"origin": "LHR",
"id": "EH10",
"rating": 0,
"destination": "FKK",
"PricingSolution":{
"TotalPrice": "USD223"
},
"Class": "Eco"
}
]
}
}',true);

echo "<pre>";
foreach($json['data']['onward'] as $k=>$v){
echo "ID :" .$v['id'];
}
echo "</pre>";
?>

and got expected output :

ID :SB15ID :EH10

Upvotes: 0

Related Questions