Php 尝试获取相同的元素值,然后应用于其他xml
这是我的general.xml文件:-Php 尝试获取相同的元素值,然后应用于其他xml,php,xml,xpath,Php,Xml,Xpath,这是我的general.xml文件:- <?xml version="1.0" encoding="UTF-8"?> <results> <numFound>10</numFound> <QTime>4</QTime> <result> <distance>1071.59873299109</distance> <name>
<?xml version="1.0" encoding="UTF-8"?>
<results>
<numFound>10</numFound>
<QTime>4</QTime>
<result>
<distance>1071.59873299109</distance>
<name>Irungattukottai</name>
</result>
<result>
<distance>1892.33578431928</distance>
<name>Valapuram</name>
</result>
</results>
此代码是工作性能,但只有一个值,例如:-general.xml
在第一个Irungattukottai
和第二个Valapuram
但我的代码是只获取第一个元素,不尝试获取wall xml文件 这是我的相应输出:-
city Name:- Irungattukottai, Entity_id:- 5079, Product_id:- 1
city Name:- Irungattukottai, Entity_id:- 5079, Product_id:- 1
city Name:- Valapuram , Entity_id:- 7140, Product_id:- 2
但是我想要这种类型的输出:-city Name:- Irungattukottai, Entity_id:- 5079, Product_id:- 1
city Name:- Irungattukottai, Entity_id:- 5079, Product_id:- 1
city Name:- Valapuram , Entity_id:- 7140, Product_id:- 2
您需要迭代$general的数组 试着去做:
echo '<pre>';
print_r($general);
echo '</pre>';
echo';
印刷(一般);
回声';
检查是否获得一个包含xml文件中所有元素的数组。获取general.xml提要的代码正常 您可以按以下方式获取名称:
$xml = simplexml_load_file( 'general.xml' );
$cities = $xml->xpath( '//result/name' );
$cityCount = count( $cities );
if( $cityCount ) {
$region = simplexml_load_file( 'region.xml' );
foreach( $cities as $city ) {
$regions = $region->xpath( '//*[@value="'. ( string ) $city . '"]/@entity_id' );
if( count ( $regions ) ) {
$product = simplexml_load_file( 'products.xml' );
$products = $product->xpath( '//dist_region[@value="' . ( string ) $regions[ 0 ][ 'entity_id' ] . '"]/ancestor::product_id/@value' );
if( count( $products ) ) {
foreach( $products as $p ) {
echo 'city Name:- ' . ( string ) $city . ', Entity_id:- ' . ( string ) $regions[ 0 ][ 'entity_id' ] . ', Product_id:- ' . $p[ 'value' ] . "\n";
}
}
}
}
}
$name1=(字符串)$general->result[0]->name;
$name2=(字符串)$general->result[1]->name;
或者可以使用循环逐个获取名称。
试试这个:
city Name:- Irungattukottai, Entity_id:- 5069, Product_id:- 1
city Name:- Irungattukottai, Entity_id:- 5069, Product_id:- 2
city Name:- Valapuram, Entity_id:- 7140, Product_id:- 2
输出
<?php
foreach($general->result as $row){
if(isset($row->name)){
//create one variable and store in value.
$name = $row->name;
//all you code for region.xml and then for product.xml
}
echo "name:- $name,Entity:- $entity, product:- $prid"."<br>";
}
?>
使用上述两种答案,可以尝试以下方法:-
尝试执行打印($general)代码>@jackphp检查我的答案。我正在尝试这样的foreach循环:-foreach($general->result->name as$row){list($entity)=$region->xpath(//*[@value='$row']/@entity\id”);$entity=(string)$entity;list($prid)=$product->xpath(///dist region[@value='$entity']/祖先::product\u id/@value”);$prid=(string)$prid;echo“城市名称:-$row,Entity\u id:-$Entity,Product\u id:-$prid”;}
如果要打印结果$general,则可以看到$general->result->Name不是数组。数组是$general->result。$general->result->Name只会给出您的名字。
city Name:- Irungattukottai, Entity_id:- 5069, Product_id:- 1
city Name:- Irungattukottai, Entity_id:- 5069, Product_id:- 2
city Name:- Valapuram, Entity_id:- 7140, Product_id:- 2
<?php
foreach($general->result as $row){
if(isset($row->name)){
//create one variable and store in value.
$name = $row->name;
//all you code for region.xml and then for product.xml
}
echo "name:- $name,Entity:- $entity, product:- $prid"."<br>";
}
?>