[英]How do multiple array object loop in JSON using PHP
我的JSON格式文件就是這樣
[
{
"closerate":"97.29",
"mcap":"112579.55263540648",
"newdate":"19-Mar-18",
"out_share":"1157.1544"
},
{
"pelast4q":"20.83297644539615",
"amount":"4.67"
}
]
我想通過循環顯示數據
foreach ($data as $nt) {
echo "<tr class='table_row_grey'>
<td ><strong>Price Date</strong></td>
<td > $nt[newdate]</td>";
echo "<tr class='table_row_white'>
<td ><strong>Close Price</strong></td>
<td >PKR</td>
<td > " . number_format($nt[closerate],2) . " </td></tr>";
echo "<tr class='table_row_grey'>
<td ><strong>Shares</strong></td>
<td >Mn</td>
<td >" . number_format($nt[out_share],2) . "</td></tr>";
echo "<tr class='table_row_white'>
<td ><strong>M.Cap</strong></td>
<td >PKR Mn</td>
<td >" . number_format($nt[mcap],0) . "</td></tr>";
echo "<tr class='table_row_grey'>
<td ><strong>P/E</strong></td>
<td >Trailing (4Q)</td>
<td > " . number_format($nt[pelast4q],2) . "</td></tr>";
}
// Close the table
echo "</table>";
我在表中兩次使用循環顯示時該怎么辦
你需要 :
$data = json_decode([{"closerate":"97.29","mcap":"112579.55263540648","newdate":"19-Mar-18","out_share":"1157.1544"},{"pelast4q":"20.83297644539615","amount":"4.67"}]);
$data_arr = file_get_contents('test.json');
$data = json_decode($data_arr, true);
$new_date = NULL;
$closerate = NULL;
$mcap = NULL;
$pelast4q = NULL;
foreach ($data as $nt) {
if (! empty($nt["newdate"])) {
$new_date = $nt["newdate"];
}
if (! empty($nt["closerate"])) {
$closerate = number_format($nt["closerate"], 2);
}
if (! empty($nt["out_share"])) {
$out_share = number_format($nt["out_share"], 2);
}
if (! empty($nt["mcap"])) {
$mcap = number_format($nt["mcap"], 0);
}
if (! empty($nt["pelast4q"])) {
$pelast4q = number_format($nt["pelast4q"], 2);
}
}
echo "<tr class='table_row_grey'>
<td ><strong>Price Date</strong></td>
<td >. $new_date.</td>";
echo "<tr class='table_row_white'>
<td ><strong>Close Price</strong></td>
<td >PKR</td>
<td > " . $closerate . " </td></tr>";
echo "<tr class='table_row_grey'>
<td ><strong>Shares</strong></td>
<td >Mn</td>
<td >" . $out_share . "</td></tr>";
echo "<tr class='table_row_white'>
<td ><strong>M.Cap</strong></td>
<td >PKR Mn</td>
<td >" . $mcap . "</td></tr>";
echo "<tr class='table_row_grey'>
<td ><strong>P/E</strong></td>
<td >Trailing (4Q)</td>
<td > " . $pelast4q . "</td></tr>";
// Close the table
echo "</table>";
首先將json轉換為數組
$data = json_decode('[ { "closerate":"97.29", "mcap":"112579.55263540648", "newdate":"19-Mar-18", "out_share":"1157.1544" }, { "pelast4q":"20.83297644539615", "amount":"4.67" } ]',true);
然后循環你的數據
echo "<table >";
foreach ($data as $nt) {
echo "<tr class='table_row_grey'> <td ><strong>Price Date</strong></td> <td > ". isset($nt['newdate']) && !empty($nt['newdate']) ? $nt['newdate'] : "-" ."</td></tr>";
echo "<tr class='table_row_white'>
<td ><strong>Close Price</strong></td>
<td >PKR</td>
<td > " . isset($nt['closerate']) && !empty($nt['closerate']) ? number_format($nt['closerate'],2) : "-" . " </td></tr>";
echo "<tr class='table_row_grey'>
<td ><strong>Shares</strong></td>
<td >Mn</td>
<td >" . isset($nt['out_share']) && !empty($nt['out_share']) ? number_format($nt['out_share'],2) : "-" . "</td></tr>";
echo "<tr class='table_row_white'>
<td ><strong>M.Cap</strong></td>
<td >PKR Mn</td>
<td >" . isset($nt['mcap']) && !empty($nt['mcap']) ? number_format($nt['mcap'],0) :"" . "</td></tr>";
echo "<tr class='table_row_grey'>
<td ><strong>P/E</strong></td>
<td >Trailing (4Q)</td>
<td > " . isset($nt['pelast4q']) && !empty($nt['pelast4q']) ? number_format($nt['pelast4q'],2) : "" . "</td></tr>";
}
// Close the table
echo "</table>";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.