dpfw3607 2016-02-01 15:52
浏览 136
已采纳

如何使用PHP将json解析为html表?

I have json file like this

[{
    "Shop_name": "916",
    "Shop_id": "916TCR",
    "Address":"cdsasffafa"
    "numbers": "4",
    "mob_no": "9447722856"
}, {
    "Shop_name": "Chicking",
    "Shop_id": "CKGTCR",
    "Address":"afagagg",
    "numbers": "8",
    "mob_no": "6767564532"
}]

i want to see this as HTML Table with row head as Shop_name,Shop_id,Address and Data in next row.

i tried decoding it and display as table (How can i parse json into a html table using PHP?). but Json File format is diffrent here. Also like to know if Json File gets bigger with [0],[1],[2]....

  • 写回答

1条回答 默认 最新

  • doubingqi5829 2016-02-01 16:04
    关注

    You should parse your json data to php object than you should iterate your data like below;

    $myData = <<<JSON
    
    [
    {"Shop_name":"minh",
      "Shop_id":"916TCR",
      "Address":"cdsasffafa",
      "numbers":"4",
      "mob_no":"9447722856"
    },
    {"Shop_name":"Chig",
     "Shop_id":"CKGTCR",
     "Address":"afagagg",
     "numbers":"8",
     "mob_no":"6767564532"
    }
    ]
    
    JSON;
    
    $myObject = json_decode($myData);
    
    ?>
    <table>
    <tr>
        <td>Shop Name</td>
        <td>Shop ID</td>
        <td>Address</td>
        <td>Numbers</td>
        <td>Mob No</td>
    </tr>
    <?PHP
    foreach($myObject as $key=>$item)
    {
        ?>
        <tr>
            <td><?PHP echo $item->Shop_name; ?></td>
            <td><?PHP echo $item->Shop_id; ?></td>
            <td><?PHP echo $item->Address; ?></td>
            <td><?PHP echo $item->numbers; ?></td>
            <td><?PHP echo $item->mob_no; ?></td>
        </tr>
        <?PHP
    }
    ?>
    </table>
    

    A working example is here: http://ideone.com/IqZLMs

    PS: You should fix this ” quote to "

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?