用PHP解析json到html表

用PHP解析json到html表,php,arrays,json,Php,Arrays,Json,我有一个数组,当我 $array = json_decode($response, true); print_r($array); 这是我从数组中得到的 Array ( [data] => Array ( [0] => Array ( [id] => accenture [type] => companies [attributes] => Array ( [name] => Accenture [description] => Accenture i

我有一个数组,当我

$array = json_decode($response, true);
print_r($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] => ) ) 
我尝试使用foreach循环创建一个表并在表中显示数据。这是我尝试的方式,但我得到一个错误“为foreach()提供的参数无效”

echo';
foreach($result作为数组){
回声';
回显'.$result->id';
回显“”。$result->name。“;
回显“.$result->description.”;
回音“.$result->资助年”;
回声';
}
回声';

谁能帮我把表格里的数据显示出来吗

HTML表的数据在
数据
数组键内。 你应该写:

foreach($array['data']作为$result){

或者更好:

$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>';
$array=json\u decode($response,true);
$resultArray=isset($array['data'])?$array['data']:[];
回声';
foreach($resultArray作为$result){
回声';
回显“”。(isset($result['id'])?$result['id']:'-');
回显“”。(isset($result['attributes']['name'])?$result['attributes']['name']:'-');
回显“”。(isset($result['attributes']['description'])?$result['attributes']['description']:'-');
回显“”。(isset($result['attributes']['funded_year'])?$result['attributes']['funded_year']:'-');
回声';
}
回声';

谢谢…现在错误消失了。但我只看到ID。在名称上我看到“-”与在描述和资助年份上一样…这是因为
name
founded\u year
更深一层。它们位于
attributes
键中。您可以使用
$result['attributes'][name']
$result['attributes]访问它们[“资助年”]
$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>';