in PHP by (15.1k points) edited by
0 like 0 dislike
1.1k views

I have the following XML file structure loaded into PHP SimpleXML:

<prospect>
	<customer>
		<name>Jacob</name>
		<name>Alex</name>
		<name>Robert</name>
	</customer>
</prospect>

How I can to get value of multiple elements same name from XML in PHP SimpleXML?


php simplexml multiple elements same name
returning multiple items with same tag via php simplexml
php simplexml receive several same tag items

1 Answer

0 like 0 dislike
by (15.1k points) edited by

You can access the different name elements by number, using array-style syntax. For obtaining tag value as string don't forget use explicit type casting to string ((string)$names[0];), else there will object of type SimpleXMLElement!

$names = $customers->prospect[0]->customer->name;

echo (string)$names[0]; // Jacob
echo (string)$names[1]; // Alex
echo (string)$names[2]; // Robert

In fact, you're already use array-style syntax for the prospect element.

Please log in or register to answer this question.