2017-03-27 48 views
1

我想回聲以下JSON響應使用PHP的HTML。JSON到Php問題

這是JSON響應:

{ 
    "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" 
     } 
    ] 
    } 
} 

這是應該出現在HTML:

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

我想它在某種程度上由總價格升序進行排序。

試圖

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

似乎並不奏效!請幫忙。

+0

嘗試'json_decode()':http://php.net/manual/en/函數.json-decode.php – JustBaron

+0

你試圖循環的數組不在'$ json ['data']' - 它在'$ json ['data'] ['onward']' – Tom

+0

Doesn' t似乎在工作!什麼不起作用,你的輸出是怎樣的?你是否收到任何錯誤或警告? –

回答

1

我已經執行的代碼,它的工作大。

入住這

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

如果你想要做排序,然後檢查此鏈接

How can I sort arrays and data in PHP?

+0

我也一樣,除了最後一個循環,我得到了預期的結果。如果你問我關閉案例 –

+0

@AmanRawat很棒!謝謝。 只要你能用一些簡單的代碼來幫助我排序的東西? 我只是一個初學者。無論如何,我們可以從TotalPrice中減去一些數字嗎?像TotalPrice - 50? – Muyi

+0

在TotalPrice'美元'是在開始,所以刪除美元,然後減去。像這樣<?= str_replace('USD','',$ item ['PricingSolution'] ['TotalPrice']) - 50; ?> –

0

使用json_decode(所以它被強制轉換爲關聯數組第二個參數爲真),以將您的JSON到PHP數組

PHP manual for json_decode

然後可以循環數組。

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

我跑這撥弄

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

,並得到了預期的輸出:

ID:SB15ID:EH10

+0

$ json = json_decode($ response,true); $輸出= 「」; foreach($ json ['data'] ['onward'] as $ data){$ output。=「ID:」。$ data ['origin']; } echo $ output;輸出是一個空白的屏幕! – Muyi

+0

以及檢查你的錯誤日誌,看看發生了什麼,我的意思是這是基本的東西。你會弄明白的。我跑了一個PHP小提琴,我得到了預期的編號 –