0

We use Zendesk at work and I am using some PHP & CURL provided by them to display data. However the Data I get comes back like this if I use (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) }

If I use just json_decode($output) I get:

object(stdClass)#1 (4) { ["satisfaction_ratings"]=> array(2) { [0]=> object(stdClass)#2 (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]=> object(stdClass)#3 (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) }

Essentially what I'm trying to do is just get a list of the "comment" sections. I tried to use this line for the json_decode($output): print $data->satisfaction_ratings->comment;

But that doesnt work, can anyone assist with getting just the one array value out?, I am not able to change the JSON that comes out of the system. The JSON before it runs through jason_decode is:

string(666) "{"satisfaction_ratings":[{"url":"https://sessioncam1384769452.zendesk.com/api/v2/satisfaction_ratings/183839137.json","id":183839137,"assignee_id":551715796,"group_id":21464896,"requester_id":543065527,"ticket_id":6,"score":"good","created_at":"2013-11-22T12:57:50Z","updated_at":"2013-11-22T12:57:50Z","comment":"hey"},{"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,"score":"good","created_at":"2013-11-22T13:16:11Z","updated_at":"2013-11-22T13:16:11Z","comment":"heyyy"}],"next_page":null,"previous_page":null,"count":2}"

If anyone can help that would be great

1
  • You can treat those decoded objects as if they were arrays anyways. $obj['satisfaction_ratings'][0]['url'] and whatnot. Commented Nov 22, 2013 at 15:09

2 Answers 2

4

If you want to access that specific element, so you can use the following:

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

If you're setting the second parameter for json_decode() as TRUE, then you need to use the array syntax to access the comment:

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

If there are multiple comments, and you want to display all of them, use a loop:

foreach ($data['satisfaction_ratings'] as $comment) {
    echo $comment['comment'] . '<br/>';
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks @Amal, would there be an easy way to work through the 2nd level? (so what im saying is to go through from 0 to whatever the last number is?)
Thanks alot, I've got it now, looking at your answer it was fairly simple all along, i'd just gotten confused.
@Stefan: Glad to have been of help!
1

Just the comments, as an array:

$data = json_decode($output, true);

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

2 Comments

Thanks for your time @bishop, ive used the other answer now though
Just don't assume that because ThereIsMoreThanOneWayToDoIt that YouShouldDoItEveryWhichWay. There is usually a good way to do it and you should do it that way :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.