Php 简单xml不支持到foreach的循环
我有以下读取XML的脚本。但它在for循环之后停止。如果我删除循环,它会运行得很好 我做错了什么,或者我可以通过其他方式实现这一点 脚本:Php 简单xml不支持到foreach的循环,php,for-loop,foreach,simplexml,Php,For Loop,Foreach,Simplexml,我有以下读取XML的脚本。但它在for循环之后停止。如果我删除循环,它会运行得很好 我做错了什么,或者我可以通过其他方式实现这一点 脚本: $xml=simplexml_load_file("test.xml"); foreach($xml->product as $child){ echo 'Title:'.$child->name.'<br>';//title echo 'Price:'.$child->price.'<br>';/
$xml=simplexml_load_file("test.xml");
foreach($xml->product as $child){
echo 'Title:'.$child->name.'<br>';//title
echo 'Price:'.$child->price.'<br>';//title
echo 'image:'.$child->images->image.'<br>';//image
echo 'category:'.$child->categories->category.'<br>';//categorie
foreach( $child->properties->property as $property ){
if( $child->properties->property->attributes()->name =='brand'){
echo $child->properties->property->value.'<br>';
}
}
}
<product ID="000000000000052993">
<name> Title product</name>
<price currency="EUR">29.99</price>
<URL>http://www.url.nl</URL>
<images>
<image>product/5/2/52993_2.jpg</image>
</images>
<description><![CDATA[description.]]>
</description>
<categories>
<category>catt</category>
</categories>
<properties>
<property name="brand">
<value>PHILIPS</value>
</property>
<property name="deliveryTime">
<value>5</value>
</property>
<property name="User1">
</property>
<property name="User2">
</property>
<property name="shipping_cost">
<value>0.00</value>
</property>
<property name="User3">
</property>
<property name="EAN">
<value>8727900831733</value>
</property>
</properties>
<variations/>
</product>
$xml=simplexml\u load\u文件(“test.xml”);
foreach($xml->productas$child){
echo“Title:”.$child->name.“
”;//Title
echo“Price:”.$child->Price.
;//标题
回显“图像:”。$child->images->image.“
”;//图像
echo“category:”.$child->categories->category.“
”;//分类
foreach($child->properties->property as$property){
如果($child->properties->property->attributes()->name=='brand'){
echo$child->properties->property->value.“
”;
}
}
}
XML:
$xml=simplexml_load_file("test.xml");
foreach($xml->product as $child){
echo 'Title:'.$child->name.'<br>';//title
echo 'Price:'.$child->price.'<br>';//title
echo 'image:'.$child->images->image.'<br>';//image
echo 'category:'.$child->categories->category.'<br>';//categorie
foreach( $child->properties->property as $property ){
if( $child->properties->property->attributes()->name =='brand'){
echo $child->properties->property->value.'<br>';
}
}
}
<product ID="000000000000052993">
<name> Title product</name>
<price currency="EUR">29.99</price>
<URL>http://www.url.nl</URL>
<images>
<image>product/5/2/52993_2.jpg</image>
</images>
<description><![CDATA[description.]]>
</description>
<categories>
<category>catt</category>
</categories>
<properties>
<property name="brand">
<value>PHILIPS</value>
</property>
<property name="deliveryTime">
<value>5</value>
</property>
<property name="User1">
</property>
<property name="User2">
</property>
<property name="shipping_cost">
<value>0.00</value>
</property>
<property name="User3">
</property>
<property name="EAN">
<value>8727900831733</value>
</property>
</properties>
<variations/>
</product>
标题产品
29.99
http://www.url.nl
product/5/2/52993_2.jpg
卡特
飞利浦
5.
0
8727900831733
让它成为另一个foreach循环:
foreach( $child->properties as $property ){
if( $property->attributes()->name == 'brand'){
echo $property->value;
}
if( $property->attributes()->name == 'deliveryTime'){
echo $property->value;
}
}
将其放入数组应该可以工作谢谢,但我想在$child->properties->property[$x]->attributes()->name上进行筛选,这是否也可以使用foreach?是的。你想达到什么目的,我会提供代码给你。我想像Loop一样过滤品牌和交付时间。不用担心,我很高兴我能帮上忙。