0

I got problem getting value from xml data, here is my data

                [a] => SimpleXMLElement Object
                    (
                        [b] => SimpleXMLElement Object
                            (
                                [id] => 123
                                [name] => Daughter
                                [a] => SimpleXMLElement Object
                                    (
                                        [b] => SimpleXMLElement Object
                                            (
                                                [id] => 234
                                                [name] => Mom 
                                                [c] => 1
                                                [a] => SimpleXMLElement Object
                                                    (
                                                        [b] => SimpleXMLElement Object
                                                            (
                                                                [id] => 345
                                                                [name] => Grandma
                                                            )

                                                    )

                                            )

                                    )

                            )

                    )

How I can get this data

123Daughter

234Mom

345Grandma

The sub child is lot not only 3 level, try to read this one XML File - Get specific child nodes in unlimited node depths but still can't understand, anyone can help me, thank you

Community
  • 1
  • 1

1 Answers1

0

You need to use the xpath function:

$xml = simplexml_load_string($xml);
foreach ($xml->xpath('//b') as $item) {
    echo $item->id . $item->name . PHP_EOL;
}

or if you want print just name:

$xml = simplexml_load_string($xml);
foreach ($xml->xpath('//name') as $name) {
    echo $name . PHP_EOL;
}

code snippet: https://3v4l.org/tYeBi#output

Mikhail
  • 452
  • 3
  • 6