Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multi Dimensional arrays in php

Tags:

arrays

php

I am new to php (learning since 1 week ). I am learning arrays. while doing it, I found a api which gives out results in the form of a multidimensional array.. and I am unable to echo the values of the array ..

Sample response

Array
(
[query] => Array
        (
            [count] => 1
            [created] => 2010-07-16T08:35:38Z
            [lang] => en-US
            [results] => Array
                (
                    [item] => Array
                        (
                            [rel] => rel:Person
                            [resource] => http://twitter.com/twitter
                            [meta] => Array
                                (
                                    [0] => Array
                                        (
                                            [property] => foaf:name
                                            [content] => Twitter
                                        )
                                )
                        )
               )
        )
 )

I can echo some values..like

echo $array["query"]['count']."<br />";   
echo $array["query"]["results"]["item"]["resource"];

but, when I want to use the [meta] => Array

I am not able to use :(

echo $array["query"]["results"]["item"]["resource"]["meta']["0"["content"];

please guide me

like image 795
ramesh Avatar asked Dec 22 '22 01:12

ramesh


1 Answers

You should use your debugging skills to tackle this kind of problem.

  • First, print_r() your $array, which you did.
  • Then print_r($array['query'])
  • Then print_r($array['query']['results'])
  • and so on, and so on

When you get to print_r($array["query"]["results"]["item"]["resource"]), you see that the result is not an array, it's a scalar, thus you need a different index.

Good luck!

like image 75
Dennis Haarbrink Avatar answered Jan 05 '23 01:01

Dennis Haarbrink