Jump to content
Stacker

Print JSON Results / Array loop

Recommended Posts

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

Share this post


Link to post
Share on other sites

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) {

 

Share this post


Link to post
Share on other sites
Posted (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 by Stacker

Share this post


Link to post
Share on other sites

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>";

 

Share this post


Link to post
Share on other sites
Posted (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 by Stacker
Blanked out email address

Share this post


Link to post
Share on other sites

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!

Share this post


Link to post
Share on other sites

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.

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.