如何使用PHP在JSON中实现多数组对象循环

如何使用PHP在JSON中实现多数组对象循环,php,html,json,Php,Html,Json,我的JSON格式文件是这样的 [ { "closerate":"97.29", "mcap":"112579.55263540648", "newdate":"19-Mar-18", "out_share":"1157.1544" }, { "pelast4q":"20.83297644539615", "amount":"4.67" } ] 我想通过循环显示数据 f

我的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>";
foreach($nt数据){
回声“
价格日期
$nt[新日期]”;
回声“
收盘价
PKR
“.number_格式($nt[closerate],2)。”;
回声“
股票
锰
“.number_格式($nt[out_share],2)。”;
回声“
M.Cap
PKR-Mn
“.number_格式($nt[mcap],0)。”;
回声“
P/E
尾随(4Q)
“.number_格式($nt[pelast4q],2)。”;
}
//合上桌子
回声“;
当我在表中使用“循环两次显示”时,我可以做什么?您需要:

$data = json_decode([{"closerate":"97.29","mcap":"112579.55263540648","newdate":"19-Mar-18","out_share":"1157.1544"},{"pelast4q":"20.83297644539615","amount":"4.67"}]);
$data\u arr=file\u get\u contents('test.json');
$data=json\u decode($data\u arr,true);
$new_date=NULL;
$closerate=NULL;
$mcap=NULL;
$pelast4q=NULL;
foreach(数据为$nt){
如果(!空($nt[“newdate”])){
$new_date=$nt[“newdate”];
}
如果(!空($nt[“closerate”])){
$closerate=数字格式($nt[“closerate”],2);
}
如果(!空($nt[“out_share”])){
$out_share=数字_格式($nt[“out_share”],2);
}
如果(!空($nt[“mcap”])){
$mcap=number_格式($nt[“mcap”],0);
}
如果(!空($nt[“pelast4q”])){
$pelast4q=数字格式($nt[“pelast4q”],2);
}
}
回声“
价格日期
.$new\u日期。“;
回声“
收盘价
PKR
" . $接近。" ";
回声“
股票
锰
" . $我们分享。"";
回声“
M.Cap
PKR-Mn
" . $麦卡普。"";
回声“
P/E
尾随(4Q)
" . $pelast4q。"";
//合上桌子
回声“;

首先将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>";
echo”“;
foreach(数据为$nt){
echo“价格日期”。isset($nt['newdate'])和&!empty($nt['newdate'])?$nt['newdate']:“-”;
回声“
收盘价
PKR
.isset($nt['closerate'])和&!empty($nt['closerate'])?数字格式($nt['closerate'],2):“-”;
回声“
股票
锰
.isset($nt['out_share'])和&!empty($nt['out_share'])?数字格式($nt['out_share'],2):“-”;
回声“
M.Cap
PKR-Mn
.isset($nt['mcap'])和&!empty($nt['mcap'])?数字格式($nt['mcap'],0):“”;
回声“
P/E
尾随(4Q)
“.isset($nt['pelast4q'])和&!empty($nt['pelast4q'])?数字格式($nt['pelast4q'],2):”;
}
//合上桌子
回声“;

到底是什么问题?(除了缺少
,列计数不一致)如何创建此json输出?当我应用loopi创建一个类似$file_name='table1.json'的json格式的文件时,表中会出现两次显示问题$mydata=json_encode($datatable1);if(file_put_contents($path,$mydata)){echo$file_name。'file Created';}我不使用if条件我在表中有很多数据显示,我想使用循环解决这个问题
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>";