2016-08-26 43 views
-1

我正在使用PHP curl從API獲取數據。我得到一個json回覆。我想在表格中回顯。這裏是我的回答是:如何將json數據加載到表中?

Array ([code] => 202 [message] => Accepted [data] => Array ([resultMap] => Array ([D2721~H16] => Array ([AppDay] => * [HosTown] => Colombo [SpecName] => Psychologist [HosName] => Ninewells Care [SpecializationId] => 36 [HosCode] => H16 [AppDate] => Any [DocName] => MS RANSIRINI DE SILVA [DoctorNo] => D2721) [D1656~H61] => Array ([AppDay] => * [HosTown] => Pannipitiya [SpecName] => Psychologist [HosName] => Pannipitiya Nursing Home [SpecializationId] => 36 [HosCode] => H61 [AppDate] => Any [DocName] => MS ACHINI RANASINGHE [DoctorNo] => D1656) [D0465~H07] => Array ([AppDay] => * [HosTown] => Colombo [SpecName] => Psychologist [HosName] => Lanka Hospitals [SpecializationId] => 36 [HosCode] => H07 [AppDate] => Any [DocName] => DR(MS) SHANEZ FERNANDO [DoctorNo] => D0465) [D1958~H44] => Array ([AppDay] => * [HosTown] => Wattala [SpecName] => Psychologist [HosName] => Hemas 


curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($rawPOSTdata)); 

// Make the REST call, returning the result 
$response = curl_exec($curl); 
if (!$response) 
    { 
    die("Connection Failure.\n"); 
    } 
// Convert the result from JSON format to a PHP array 

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

curl_close($curl); 
if (isset($result->error)) 
    { 
    die($result->error_message."\n"); 
    } 
} 

回答

0

我的第一個猜想是通過與foreach語句的數組遍歷:

if(is_array($result) && !empty($result)) { // check if we have some lines 
    echo '<table>'; 

    foreach($result as $elem) { // iterate through all the elements 
     echo '<tr>'; 
     foreach($elem as $tdValue) { // output all field values (if you want to output all of course) 
      echo '<td>' . $tdValue . '</td>'; 
     } 
     echo '</tr>'; 
    } 

    echo '</table>'; 
}