Manish Jangir
Manish Jangir

Reputation: 5437

how to fetch this array using foreach loop

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

Upvotes: 1

Views: 1985

Answers (2)

Benjamin Crouzier
Benjamin Crouzier

Reputation: 41895

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();
}

Upvotes: 1

Vikk
Vikk

Reputation: 3363

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...
}

Upvotes: 2

Related Questions