我试图从api获取一些数据并循环通过一组数组,但我无法这样做因为它一直给出错误,即注意:数组转换为字符串
我的代码是:
<?php
$url = 'myapiurl';
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_POSTFIELDS => "",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"cache-control: no-cache",
"content-type: application/json"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
}
else {
$characters = json_decode($response, true);
$x = ($characters["data"]);
echo $x;
// print_r($x);
//var_dump($characters);
}
?>
Var_dump结果:
array(4) { ["data"]=> array(6) { [0]=> array(5) { ["ticket_status_id"]=> string(1) "0" ["ticket_id"]=> string(5) "28597" ["priority_id"]=> string(1) "0" ["created_at"]=> string(19) "2018-05-17 15:46:07" ["status_changed_at"]=> NULL } [1]=> array(5) { ["ticket_status_id"]=> string(1) "5" ["ticket_id"]=> string(5) "28598" ["priority_id"]=> string(1) "0" ["created_at"]=> string(19) "2018-05-17 15:49:07" ["status_changed_at"]=> string(19) "2018-05-17 10:21:21" } [2]=> array(5) { ["ticket_status_id"]=> string(1) "0" ["ticket_id"]=> string(5) "28668" ["priority_id"]=> string(1) "0" ["created_at"]=> string(19) "2018-05-18 09:47:28" ["status_changed_at"]=> NULL } [3]=> array(5) { ["ticket_status_id"]=> string(1) "0" ["ticket_id"]=> string(5) "28669" ["priority_id"]=> string(1) "0" ["created_at"]=> string(19) "2018-05-18 09:47:48" ["status_changed_at"]=> NULL } [4]=> array(5) { ["ticket_status_id"]=> string(1) "5" ["ticket_id"]=> string(5) "29036" ["priority_id"]=> string(1) "3" ["created_at"]=> string(19) "2018-05-22 12:13:03" ["status_changed_at"]=> string(19) "2018-05-22 06:43:51" } [5]=> array(5) { ["ticket_status_id"]=> string(1) "5" ["ticket_id"]=> string(5) "29037" ["priority_id"]=> string(1) "3" ["created_at"]=> string(19) "2018-05-22 12:15:29" ["status_changed_at"]=> NULL } } ["code"]=> int(200) ["error"]=> string(5) "false" ["message"]=> string(7) "Success" }
Json的回应:
{"data":[{"ticket_status_id":"0","ticket_id":"28597","priority_id":"0","created_at":"2018-05-17 15:46:07","status_changed_at":null},{"ticket_status_id":"5","ticket_id":"28598","priority_id":"0","created_at":"2018-05-17 15:49:07","status_changed_at":"2018-05-17 10:21:21"},{"ticket_status_id":"0","ticket_id":"28668","priority_id":"0","created_at":"2018-05-18 09:47:28","status_changed_at":null},{"ticket_status_id":"0","ticket_id":"28669","priority_id":"0","created_at":"2018-05-18 09:47:48","status_changed_at":null},{"ticket_status_id":"5","ticket_id":"29036","priority_id":"3","created_at":"2018-05-22 12:13:03","status_changed_at":"2018-05-22 06:43:51"},{"ticket_status_id":"5","ticket_id":"29037","priority_id":"3","created_at":"2018-05-22 12:15:29","status_changed_at":null}],"code":200,"error":"false","message":"Success"}
如何从此回复ticket_id和created_at?
答案 0 :(得分:1)
foreach($data as $key => $value) {
echo $value["ticket_id"];
echo "<br/>";
echo $value["created_at"];
}