Jump to content

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

Link to comment
https://forums.phpfreaks.com/topic/310462-print-json-results-array-loop/
Share on other sites

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

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!

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
  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

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.