Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php simplexml,返回具有相同标记的多个项_Php_Xml_Simplexml - Fatal编程技术网

Php simplexml,返回具有相同标记的多个项

Php simplexml,返回具有相同标记的多个项,php,xml,simplexml,Php,Xml,Simplexml,我已将以下XML文件加载到php simplexml中 <adf> <prospect> <customer> <name part="first">Bob</name> <name part="last">Smith</name> </customer> </prospect> </adf> 这将返回“Bob”,但如何返回姓氏 echo $customers->pro

我已将以下XML文件加载到php simplexml中

<adf>
<prospect>
<customer>
<name part="first">Bob</name>
<name part="last">Smith</name>
</customer>
</prospect>
</adf>
这将返回“Bob”,但如何返回姓氏

echo $customers->prospect[0]->customer->contact->name;

您可以使用数组样式语法按编号访问不同的
元素

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

echo $names[0]; // Bob
echo $names[1]; // Smith
事实上,您已经在为
元素执行此操作了

另请参见手册中的


如果要根据某些条件选择图元,则可以使用以下工具

$customer   = $customers->prospect[0]->customer;
$last_names = $customer->xpath('name[@part="last"]'); // always returns an array
echo $last_names[0]; // Smith

谢谢,正是我想要的!
$customer   = $customers->prospect[0]->customer;
$last_names = $customer->xpath('name[@part="last"]'); // always returns an array
echo $last_names[0]; // Smith