dongyumiao5210 2017-09-03 15:37
浏览 61
已采纳

在PHP中将json解析为html表

I have an array and when I do

$array = json_decode($response, true);
print_r($array);

This is what I get from an array.

Array ( [data] => Array ( [0] => Array ( [id] => accenture [type] => companies [attributes] => Array ( [name] => Accenture [description] => Accenture is a global management consulting, technology services and outsourcing company, with more than 293,000 people serving clients in more than 120 countries. Combining unparalleled experience, comprehensive capabilities across all industries and business functions, and extensive research on the world’s most successful companies, Accenture collaborates with clients to help them become high-performance businesses and governments. The company generated net revenues of US$xxx for the fiscal year ended Aug. 31, 2013. Its home page is www.some-url.com. [employee_count_range] => 10001+ [founded_year] => 1989 [industries] => Array ( [0] => Information Technology & Services ) [website_url] => www.some-url.com [logo] => https://some-url/logo.png [square_logo] => https://some-url/square_logo.png [followed] => [claimed_status] => 1 [last_reviewed_at] => ) ) [1] => Array ( [id] => accenture-banglore [type] => companies [attributes] => Array ( [name] => Accenture, Banglore [description] => [employee_count_range] => [founded_year] => [industries] => [website_url] => [logo] => [square_logo] => [followed] => [claimed_status] => 1 [last_reviewed_at] => ) ) [2] => Array ( [id] => accenture-gmbh [type] => companies [attributes] => Array ( [name] => Accenture GmbH [description] => [employee_count_range] => [founded_year] => [industries] => [website_url] => [logo] => [square_logo] => [followed] => [claimed_status] => 1 [last_reviewed_at] => ) ) 

I tried with foreach loop to create a table and show the data in the table. This is how I try but I get an error "Invalid argument supplied for foreach()"

echo '<table>';
foreach($array as $result){
  echo '<tr>';
echo '<td>'.$result->id.'</td>';
echo '<td>'.$result->name.'</td>';
echo '<td>'.$result->description.'</td>';
echo '<td>'.$result->funded_year.'</td>';
  echo '</tr>';
}
echo '</table>';

Can anyone help me show the data in the table?

  • 写回答

1条回答 默认 最新

  • duanpi7107 2017-09-03 15:41
    关注

    Your data for HTML table is inside data array key. You should write:

    foreach($array['data'] as $result){

    or even better:

    $array = json_decode($response, true);
    $resultArray = isset($array['data']) ? $array['data'] : [];
    
    echo '<table>';
    foreach($resultArray as $result){
            echo '<tr>';
            echo '<td>'.(isset($result['id']) ? $result['id'] : '-') .'</td>';
            echo '<td>'.(isset($result['attributes']['name']) ? $result['attributes']['name'] : '-').'</td>';
            echo '<td>'.(isset($result['attributes']['description']) ? $result['attributes']['description'] : '-').'</td>';
            echo '<td>'.(isset($result['attributes']['funded_year']) ? $result['attributes']['funded_year'] : '-').'</td>';
            echo '</tr>';
    }
    echo '</table>';
    

    http://php.net/manual/en/function.json-decode.php

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

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器