Stacker Posted April 4, 2020 Share Posted April 4, 2020 Hello, Having a bit of a tough time understanding what is going on in my brain! I'm basically pulling some data from an API using CuRL, which is working fine. The structure of the data is: [data][tickets] - tickets being an array of support tickets in the system, which then contains ticketNumber, title, content. I want to display these values in a list, I have this code currently: if ($err) { echo "cURL Error #:" . $err; } else { $return = json_decode($response, true); foreach($return as $key) { echo $key["tickets"][1]['ticketNumber']." - "; echo $key["tickets"][1]['title']." - "; echo $key["tickets"][1]['content']; } } Which obviously returns the ticketNumber, title & content of the first support ticket in the array, but I want to loop through and display that information for each ticket! Can anyone advise as to where I am going wrong? I think I need to utilise the ["tickets"][x] but not sure how! Thanks in advance! Steve Quote Link to comment Share on other sites More sharing options...
gw1500se Posted April 4, 2020 Share Posted April 4, 2020 Do a 'var_dump($return)' to see exactly how the decode structures the return array. From that you will see what your error is. As a hint, your foreach statement should look more like: foreach ($return as $key=>$val) { Quote Link to comment Share on other sites More sharing options...
Stacker Posted April 4, 2020 Author Share Posted April 4, 2020 (edited) Thanks for the fast reply! This is the data I get back from a variable dump of return: Quote Skip this! So I need to be in data -> tickets the rest of the info is irrelevant! Edited April 4, 2020 by Stacker Quote Link to comment Share on other sites More sharing options...
gw1500se Posted April 4, 2020 Share Posted April 4, 2020 You need to learn to use these functions. Notice how unreadable your output is. If you RTFM you would see that var_dump outputs a structured format. Try again with: echo "<pre>"; var_dump($return); echo "</pre>"; Quote Link to comment Share on other sites More sharing options...
Stacker Posted April 4, 2020 Author Share Posted April 4, 2020 (edited) Sorry my bad, let me try that again! array(2) { ["status"]=> string(7) "success" ["data"]=> array(6) { ["name"]=> string(6) "Steven" ["email"]=> string(26) "xxx" ["staff"]=> bool(false) ["verified"]=> bool(true) ["tickets"]=> array(2) { [0]=> array(17) { ["ticketNumber"]=> string(6) "723789" ["title"]=> string(16) "my second ticket" ["content"]=> string(48) "This is my not my first ticket, I hope it works" ["department"]=> array(2) { ["id"]=> string(1) "1" ["name"]=> string(7) "Support" } ["date"]=> string(12) "202004031053" ["file"]=> NULL ["language"]=> string(2) "en" ["unread"]=> bool(false) ["unreadStaff"]=> bool(true) ["closed"]=> bool(false) ["priority"]=> string(3) "low" ["author"]=> array(6) { ["id"]=> string(1) "2" ["name"]=> string(6) "Steven" ["staff"]=> bool(false) ["profilePic"]=> NULL ["email"]=> string(26) "xx" ["customfields"]=> array(0) { } } ["owner"]=> NULL ["events"]=> array(0) { } ["tags"]=> array(0) { } ["edited"]=> NULL ["editedTitle"]=> NULL } [1]=> array(17) { ["ticketNumber"]=> string(6) "318070" ["title"]=> string(16) "my second ticket" ["content"]=> string(48) "This is my not my first ticket, I hope it works" ["department"]=> array(2) { ["id"]=> string(1) "1" ["name"]=> string(7) "Support" } ["date"]=> string(12) "202004031100" ["file"]=> NULL ["language"]=> string(2) "en" ["unread"]=> bool(false) ["unreadStaff"]=> bool(true) ["closed"]=> bool(false) ["priority"]=> string(3) "low" ["author"]=> array(6) { ["id"]=> string(1) "2" ["name"]=> string(6) "Steven" ["staff"]=> bool(false) ["profilePic"]=> NULL ["email"]=> string(26) "xx" ["customfields"]=> array(0) { } } ["owner"]=> NULL ["events"]=> array(0) { } ["tags"]=> array(0) { } ["edited"]=> NULL ["editedTitle"]=> NULL } } ["customfields"]=> array(0) { } } } Thank you Edited April 4, 2020 by Stacker Blanked out email address Quote Link to comment Share on other sites More sharing options...
Stacker Posted April 4, 2020 Author Share Posted April 4, 2020 Ok I've managed to work it out, the answer was this: $return = json_decode($response, true); foreach($return as $key) { foreach($key['tickets'] as $ticket) { echo $ticket['ticketNumber']."-"; echo $ticket['title']."-"; echo $ticket['content']; echo '<br />'; } } Cheers! Quote Link to comment Share on other sites More sharing options...
maxxd Posted April 4, 2020 Share Posted April 4, 2020 You should be getting at least a notification as the 'status' array in the response doesn't have a 'tickets' index. Check your data nodes before attempting to output anything, and make the code more explicit in what it's trying to do: $return = json_decode($response, true); if($return['status'] == 'success' && !empty($return['data']['tickets'])){ foreach($return['data']['tickets'] as $ticket){ echo $ticket['ticketNumber'].' - '; echo $ticket['title'].' - '; echo $ticket['content'].'<br />'; } } If the call fails, I assume 'status' will not be 'success' and 'tickets' will be empty - the above will stop your program from crashing at that point. 1 Quote Link to comment Share on other sites More sharing options...
phpguru123 Posted April 22, 2020 Share Posted April 22, 2020 hi you can see what is coming in json array by simping putting json on json decode online here you can see json as array Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.