1

i have a multidimensional array. the array is returned by parsing xml using curl. when curl gave me the output i converted the output into array using $array = (array) simplexml_load_string($query); and the $array is given below. Now i want to fetch this array using foreach loop and want everything from this array

Array
(
[Meta] => SimpleXMLElement Object
    (
      [Query] => php programming
      [ResultOffset] => SimpleXMLElement Object
            (
            )

      [NumResults] => 25
      [TotalResults] => 36839
    )

[Slideshow] => Array
       (
        [0] => SimpleXMLElement Object
            (
                [ID] => 1966058
                [Title] => title here
                [Description] => description here
                [Status] => 2
                [Username] =>usrname
                [URL] => url here
                [ThumbnailURL] => a url
                [ThumbnailSmallURL] => a url
                [Embed] => some embed code
    )
    [1] => SimpleXMLElement Object
            (
                [ID] => 1966058
                [Title] => title here
                [Description] => description here
                [Status] => 2
                [Username] =>usrname
                [URL] => url here
                [ThumbnailURL] => a url
                [ThumbnailSmallURL] => a url
                [Embed] => some embed code
    )

and continue

2
  • What information you need to have acces in your loop ? For example: Status and Username. Commented Nov 5, 2011 at 22:00
  • i want everything from this array Commented Nov 6, 2011 at 2:34

2 Answers 2

2

You can retrieve meta information without using foreach:

echo $array['Meta']->Query;
echo $array['Meta']->NumResults;

and so on...

To fetch slideshows:

foreach($array['Slideshow'] as $slideshow)
{
    echo $slideshow->ID;
    echo $slideshow->Title;
    //-- and so on...
}
Sign up to request clarification or add additional context in comments.

3 Comments

Warning: Invalid argument supplied for foreach()
echo $array['Meta']->Query; echo$array['Meta']->NumResults; This is working fine but the another code in giving a warning that "Warning: Invalid argu" Help please
There can't be such warning if your $array is same as the one you posted in question.
1

If you want to retrieve the ID and Titles of each SimpleXMLElement Object, try this:

<?php
forach ($array['Slideshow'] as $simpleXMLelem) {
  echo $simpleXMLelem->getId();
  echo $simpleXMLelem->getTitle();
}

1 Comment

@rajzana: Well you need to fill in getArray() with your own logic. Some effort required.

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.