Craig Brass Systems Forum: Looping Through Arrays with PHP - Craig Brass Systems Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Looping Through Arrays with PHP Need a little help with the Kayako API

#1
User is offline   Chad 

  • Member
  • Pip
  • Group: Members
  • Posts: 1
  • Joined: 30-May 09
Firstly, the Kayako API is the best, I'm very happy you've created it and grateful for it as it will serve our needs perfectly.

My PHP skills are not that good when it comes to dealing with Arrays, so I'm requesting a little assistance. If someone could provide me with some example code that would be great.

I need to store the values of the array keys in a variable.

For example, the API is giving me back the following when using the getSubmittedTicket function.

CODE
Array
(
    [id] => 1
    [mask] => MLY-633303
    [department] => 1
    [status] => 1
    [priority] => 1
    [startdate] => 1243844175
    [lastactivity] => 1243848115
    [subject] => Test Ticket
    [posts] => Array
        (
            [0] => Array
                (
                    [id] => 1
                    [date] => 1243844175
                    [userid] => 1
                    [fullname] => Carolina Johnson
                    [email] => cmeneses89@yahoo.com
                    [edited] =>
                    [editedbystaffid] => 0
                    [editeddate] => 0
                    [ishtml] =>
                    [staffid] => 0
                    [contents] => Simply a test ticket
                    [attachments] => Array
                        (
                        )

                )

            [1] => Array
                (
                    [id] => 2
                    [date] => 1243848115
                    [userid] => 0
                    [fullname] => Chris Hankins
                    [email] => sales@afakedomain.com
                    [edited] =>
                    [editedbystaffid] => 0
                    [editeddate] => 0
                    [ishtml] =>
                    [staffid] => 1
                    [contents] => Everything is fine on this account, please take a look at it and stop being a idiot.<br />
<br />
Thanks,<br />
Chris
                    [attachments] => Array
                        (
                        )

                )

        )

)




The PHP code I'm using wont actually give back the "Post" only the ticket details at the beginning.

I'm looking to do something like this . . .

CODE
<?
echo "$result[fullname][0]"
?>


This would then echo "Carolina Johnson".


Can someone please give me some advise on looping through this information and storing the values in variables?

Thanks!
0

#2
User is offline   Andrew Gillard 

  • Staff - Lead Developer
  • Pip
  • Group: Developers
  • Posts: 97
  • Joined: 04-March 07
  • Gender:Male
  • Location:Basingstoke, United Kingdom
I'm not 100% sure of what you want to do, but I'll take a guess. Let me know if I don't quite answer your question smile.gif

When accessing elements of a multi-dimensional array (i.e. an array containing other arrays), you need to provide the keys in the order that they're displayed in the var_dump() output that you posted. In this case, the full name of the ticket poster would be:
CODE
echo $result['posts'][0]['fullname'];


If you're wanting to access details of every single post in the ticket, the best way of doing that would be using the "foreach" statement, which exceutes a block of code for every element of an array:
CODE
foreach ($result['posts'] as $post) {
    echo $post['fullname'] . "<br />\r\n";
}

In this example, $post will be the numbered array elements of the "posts" array - i.e. it will have all of the id, date, userid, fullname, email, etc. elements that are accessible by using $result['posts'][0]['....'].

Hope this helps.
Andrew Gillard
Lead Developer - Craig Brass Systems
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users