Miguel Angel Mendoza
Miguel Angel Mendoza

Reputation: 1312

SimpleXMLElement php, foreach

I have this xml object, and I want to get each "p" $xml->query->search["p"][0]->attribute["title"], how can get each one using foreach?

SimpleXMLElement Object
(
    [query] => SimpleXMLElement Object
        (
            [search] => SimpleXMLElement Object
                (
                    [p] => Array
                        (
                            [0] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [ns] => 0
                                            [title] => Amebiasis
                                            [timestamp] => 2013-09-16T16:11:24Z
                                        )
                                )
                            [1] => SimpleXMLElement Object
                                (
                                    [@attributes] => Array
                                        (
                                            [ns] => 0
                                            [title] => Entamoeba histolytica
                                            [timestamp] => 2013-09-10T19:59:49Z
                                        )
                                )
                        )
                )
        )
)

Upvotes: 0

Views: 62

Answers (1)

Amine Matmati
Amine Matmati

Reputation: 283

you can use xpath like this

$titles = $o->xpath('query/search/p/@title');

Or to get all the title attributes in the document

$titles = $o->xpath('//@title');

Upvotes: 1

Related Questions