0

I'm trying to get info out of this information:

Array (
    [result] => success
    [totalresults] => 1
    [startnumber] => 0
    [numreturned] => 1 
    [tickets] => Array (
        [ticket] => Array (
            [0] => Array (
                [id] => 7
                [tid] => 782755
                [deptid] => 1
                [userid] => 39
                [name] => Mark Lønquist
                [email] => [email protected]
                [cc] => 
                [c] => 79rzVBeJ
                [date] => 2013-04-25 16:14:24
                [subject] => test
                [status] => Open
                [priority] => Medium
                [admin] => 
                [attachment] => 
                [lastreply] => 2013-04-25 16:14:24 
                [flag] => 0
                [service] => 
            )
        )
    )
)

The results are printed using:

print_r($results);

Usually, I've been able to do a simple:

$var = $results['something'];

To get it out, but it wont work with this :( Any help is appreciated.

11
  • $results['something'] should work. Can you be more specific? Is there a particular something? Commented Apr 26, 2013 at 1:36
  • Add this to the top off your code: error_reporting(E_ALL); and see what the problem is... Commented Apr 26, 2013 at 1:37
  • I refuse to believe it won't work with this - as others have pointed out, show the errors you are getting. Commented Apr 26, 2013 at 1:41
  • if $results['something'] won't work, then you need $results['something']['somethingElse'] Commented Apr 26, 2013 at 1:44
  • I've updated the indentation to make it clearer. Perhaps this will shed light on the issue. Commented Apr 26, 2013 at 1:45

2 Answers 2

1

After reformatting the array you pasted, it becomes clear that some elements are nested several levels deep. (It's a "multidimensional array"; see example #6 in the docs.) In those cases, you have to add additional brackets containing each successive key to reach the depth you want. For example, a sample from your $results array:

Array (
    [result] => success
    [totalresults] => 1
    ...
    [tickets] => Array (
        [ticket] => Array (
            [0] => Array (
                [id] => 7
                [tid] => 782755
                ...
            )
        )
    )
)

You simply need to do $results['totalresults'] to access "totalresults", but to get "tid" you would need to use $results['tickets']['ticket'][0]['tid'].


If you want to get "tid" from all of the tickets when there are multiple, you will have to iterate (loop) over the array of tickets. Probably something like this (untested, but should be close enough for you to figure out):

foreach ($results['tickets']['ticket'] as $ticket) {
    echo $ticket['tid'];
}
Sign up to request clarification or add additional context in comments.

Comments

0

To see what the problem is with your print_r() you may add error_reporting(E_ALL); to the top of your code.

Note that if you want to retrieve the value for a key such as 'totalresults' then $results['totalresults'] would be sufficient.

However, if you want to get a key from one of the nested arrays such as email then you would have to use $results['result']['tickets']['ticket'][0]['email'].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.