duanmo5724 2016-02-25 14:26
浏览 58
已采纳

无法将JSON解码为PHP数组并在HTML表格中显示

I have this json data

{
   "next_offset": -1,
   "records":    [
            {
         "id": "87dad127-a60e-15a3-148e-56c7675f11df",
         "name": "1A Unit",
         "suite": "1A",
         "sf": 1200,
         "unit_floor": 1,
      },
            {
         "id": "f386a7ad-d6ea-6eb1-9b8f-56cd6104c445",
         "name": "3B Unit",
         "suite": "3B",
         "sf": 450,
         "unit_floor": 3,
      },
            {
         "id": "b352dc84-8c76-6c27-daa4-56cd61e71aa2",
         "name": "3A Unit",
         "suite": "3A",
         "sf": 450,
         "unit_floor": 3,
      },
            {
         "id": "8ec4325a-1271-560e-888b-56cd6120f5de",
         "name": "2B Unit",
         "suite": "2B",
         "sf": 450,
         "unit_floor": 2,
      },
            {
         "id": "5a15fd5e-246a-be4b-fd5d-56cd619e9ee1",
         "name": "1B Unit",
         "suite": "1B",
         "sf": 450,
         "unit_floor": 1,
      },
            {
         "id": "61a55092-5683-1088-2d6c-56c99c5d4873",
         "name": "2A Unit",
         "suite": "2A",
         "sf": 3000,
         "unit_floor": 2,
      }
   ]
}

I want to display this data in table depending on the floor number,. Like 3rd floor units 3A, 3B and total of their area in sqft in one row, 2nd floor units 2A, 2B and their sum of area in one row and 1st floor units 1A, 1B and their area sum in one row. Please help me.

I tried this but its not giving the expected result

$unit_response = call($url, $oauth2_token_response->access_token, 'GET');

        $unit_json = json_decode(json_encode($unit_response),true);
    <table class="stak-plan">

                <?php foreach ($unit_json['records'] as $unit_data) { ?>

                <tr>
                    <td>Floor: 3</td>
                    <td> 
                        Suit : <?php echo $unit_data['name'] ?><br>
                        SF : <?php echo $unit_data['sf'] ?>
                    </td>
                    <td>Suite: 3B<br /> SF: 25,000</td>
                    <td>Suite: 3C<br /> SF: 25,000</td>
                    <td> 
                    </td>
                </tr>  
                <?php } ?>

                </table>

Here is the expected output,. Output

  • 写回答

5条回答 默认 最新

  • dpn517111 2016-02-25 15:01
    关注

    CSS:

    <style>
        td{
            border: 1px solid black;
            padding: 15px;
        }
    </style>
    

    JSON (some extra commas has been removed):

    $json = '{
        "next_offset": -1,
        "records":    [
                 {
              "id": "87dad127-a60e-15a3-148e-56c7675f11df",
              "name": "1A Unit",
              "suite": "1A",
              "sf": 1200,
              "unit_floor": 1
           },
                 {
              "id": "f386a7ad-d6ea-6eb1-9b8f-56cd6104c445",
              "name": "3B Unit",
              "suite": "3B",
              "sf": 450,
              "unit_floor": 3
           },
                 {
              "id": "b352dc84-8c76-6c27-daa4-56cd61e71aa2",
              "name": "3A Unit",
              "suite": "3A",
              "sf": 450,
              "unit_floor": 3
           },
                 {
              "id": "8ec4325a-1271-560e-888b-56cd6120f5de",
              "name": "2B Unit",
              "suite": "2B",
              "sf": 450,
              "unit_floor": 2
           },
                 {
              "id": "5a15fd5e-246a-be4b-fd5d-56cd619e9ee1",
              "name": "1B Unit",
              "suite": "1B",
              "sf": 450,
              "unit_floor": 1
           },
                 {
              "id": "61a55092-5683-1088-2d6c-56c99c5d4873",
              "name": "2A Unit",
              "suite": "2A",
              "sf": 3000,
              "unit_floor": 2
           }
        ]
     }';
    

    PHP:

        $jd = json_decode($json);
        $floors = array();
        foreach ($jd->records AS $key => $obj) {
            $f = $obj->unit_floor;
            $id = $obj->id;
            $name = $obj->name;
            $suite = $obj->suite;
            $sf = $obj->sf;
            $floors[$f][$suite]['name'] = $name;
            $floors[$f][$suite]['id'] = $id;
            $floors[$f][$suite]['sf'] = $sf;
        }
        //sort floors in desc order
        krsort($floors);
        foreach($floors as $id => $floor){
            ksort($floors[$id]);
        }
        print '<table>';
        foreach($floors as $floor => $suites){
            $sqf = 0;
            print '<tr>';
                print '<td>FLOOR: '.$floor.'</td>';
                foreach($suites AS $suite => $value){
                    $sqf += $value['sf'];
                    print '<td>Suite: '.$suite.'<br>SF: '.$value['sf'].'</td>';
                }
                print '<td>'.$sqf.'</td>';
            print '</tr>';
        }
        print '</table>';
    

    OUTPUT:

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启