2017-09-03 55 views
0

HTML表我有一個數組,當我做解析JSON在PHP中

$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 '<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>'; 

誰能幫我在表格中顯示的數據?

回答

1

您的HTML表格數據在data數組鍵中。 你應該寫:

foreach($array['data'] as $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>'; 

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

+0

感謝...現在錯誤消失。但我只看到身份證。在名字上,我看到「 - 」與描述和funded_year一樣...... – Barlet

+0

這是因爲'name'和'founded_year'是更深一層。他們在'屬性'鍵中。你可以用'$ result ['attributes'] [name']'和'$ result ['attributes'] ['funded_year']來訪問它們' –