如何将json数据加载到表中?

时间:2016-08-26 11:03:24

标签: php arrays json curl

我正在使用php curl从API获取数据。我收到了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");
    } 
 }

1 个答案:

答案 0 :(得分: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>';
}