in PHP by (12.8k points) edited by
0 like 0 dislike
6 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 (12.8k 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.

Your answer

Try to answer the question as detailed as possible.
Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.