将JSON转换为PHP数组

时间:2013-11-22 15:04:12

标签: php arrays json

我们在工作中使用Zendesk,而我正在使用一些PHP&由他们提供的CURL来显示数据。然而,如果我使用(json_decode($ output,true)),我得到的数据会像这样回来:

  

array(4){[“satisfaction_ratings”] => array(2){[0] => array(10){[“url”] => string(83)“https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/183839137.json”[“id”] => int(183839137)[“assignee_id”] => int(551715796)[“group_id”] => int(21464896)[“requester_id”] => int(543065527)[“ticket_id”] => int(6)[“score”] => string(4)“good”[“created_at”] => string(20)“2013-11-22T12:57:50Z”[“updated_at”] => string(20)“2013-11-22T12:57:50Z”[“comment”] => string(3)“hey”} [1] => array(10){[“url”] => string(83)“https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/180738208.json”[“id”] => int(180738208)[“assignee_id”] => int(551715796)[“group_id”] => int(21464896)[“requester_id”] => int(543078357)[“ticket_id”] => int(7)[“score”] => string(4)“good”[“created_at”] => string(20)“2013-11-22T13:16:11Z”[“updated_at”] => string(20)“2013-11-22T13:16:11Z”[“comment”] => string(5)“heyyy”}} [“next_page”] => NULL [“previous_page”] => NULL [“count”] => int(2)}

如果我只使用json_decode($ output),我得到:

  

object(stdClass)#1(4){[“satisfaction_ratings”] => array(2){[0] =>   object(stdClass)#2(10){[“url”] =>串(83)   “https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/183839137.json”   [ “ID”] => int(183839137)[“assignee_id”] => INT(551715796)   [ “GROUP_ID”] => int(21464896)[“requester_id”] => INT(543065527)   [ “TICKET_ID”] => int(6)[“score”] => string(4)“good”[“created_at”] =>   string(20)“2013-11-22T12:57:50Z”[“updated_at”] =>串(20)   “2013-11-22T12:57:50Z”[“comment”] => string(3)“hey”} [1] =>   object(stdClass)#3(10){[“url”] =>串(83)   “https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/180738208.json”   [ “ID”] => int(180738208)[“assignee_id”] => INT(551715796)   [ “GROUP_ID”] => int(21464896)[“requester_id”] => INT(543078357)   [ “TICKET_ID”] => int(7)[“score”] => string(4)“good”[“created_at”] =>   string(20)“2013-11-22T13:16:11Z”[“updated_at”] =>串(20)   “2013-11-22T13:16:11Z”[“comment”] => string(5)“heyyy”}}   [ “next_page”] => NULL [“previous_page”] => NULL [“count”] => int(2)}

基本上我要做的就是获取“评论”部分的列表。我尝试将此行用于json_decode($ output):print $ data-> satisfaction_ratings-> comment;

但是这不起作用,任何人都可以协助只获取一个数组值吗?我无法更改系统出来的JSON。在jason_decode运行之前的JSON是:

  

的字符串(666)   “{” satisfaction_ratings “:[{” URL “:”https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/183839137.json”, “标识”:183839137 “assignee_id”:551715796 “GROUP_ID”:21464896, “requester_id”:543065527 “TICKET_ID”:6, “分数”: “好”, “created_at”: “2013-11-22T12:57:50Z”, “的updated_at”: “2013-11-22T12:57:50Z”, “评论”: “哎”},{ “URL”: “https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/180738208.json”, “ID”:180738208, “assignee_id”:551715796, “GROUP_ID”:21464896, “requester_id”:543078357, “TICKET_ID”:7, “分数”: “好”, “created_at”: “2013-11-22T13:16:11Z”, “的updated_at”: “2013-11-22T13:16:11Z”, “评论”: “heyyy”}], “next_page”:空,“previous_page “:NULL,” 计数 “:2}”

如果有人可以提供帮助那就太棒了

2 个答案:

答案 0 :(得分:3)

如果您想访问特定的元素,那么您可以使用以下内容:

echo $data->satisfaction_ratings[0]->comment;
echo $data->satisfaction_ratings[1]->comment;

如果您将json_decode()的第二个参数设置为TRUE,则需要使用数组语法来访问注释:

echo $data['satisfaction_ratings'][0]['comment'];
echo $data['satisfaction_ratings'][1]['comment'];

如果有多条评论,并且您想要显示所有评论,请使用循环:

foreach ($data['satisfaction_ratings'] as $comment) {
    echo $comment['comment'] . '<br/>';
}

答案 1 :(得分:0)

只是评论,作为数组:

$data = json_decode($output, true);

$comments = array_map(
    function ($s) { return $s['comment']; },
    $data['satisfaction_ratings']
);