Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

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

share|improve this question
    
You can treat those decoded objects as if they were arrays anyways. $obj['satisfaction_ratings'][0]['url'] and whatnot. – Marc B Nov 22 '13 at 15:09
up vote 3 down vote accepted

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/>';
}
share|improve this answer
    
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?) – Stefan Nov 22 '13 at 15:10
    
@Stefan: See the updated answer. – Amal Murali Nov 22 '13 at 15:12
    
Thanks alot, I've got it now, looking at your answer it was fairly simple all along, i'd just gotten confused. – Stefan Nov 22 '13 at 15:14
    
@Stefan: Glad to have been of help! – Amal Murali Nov 22 '13 at 15:15

Just the comments, as an array:

$data = json_decode($output, true);

$comments = array_map(
    function ($s) { return $s['comment']; },
    $data['satisfaction_ratings']
);
share|improve this answer
    
Thanks for your time @bishop, ive used the other answer now though – Stefan Nov 22 '13 at 15:15
    
Sounds good, always TIMTOWTDI! – bishop Nov 22 '13 at 15:16
    
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 :) – Amal Murali Nov 22 '13 at 15:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.