问题描述
这是我的reg.xml:
<childrens>
<child_4 entity_id="4" value="Activities" parent_id="2">
<child_10066 entity_id="10066" value="Physical1" parent_id="4">
<child_10067 entity_id="10067" value="Cricket" parent_id="10066">
<child_10068 entity_id="10068" value="One Day" parent_id="10067"/>
</child_10067>
</child_10066>
</child_4>
<child_4331 entity_id="4331" value="Region" parent_id="2">
<child_5069 entity_id="5069" value="Irungattukottai" parent_id="4331"/>
</child_4331>
</childrens>
这是我的 product.xml
文件:
This is my product.xml
file:
<products>
<product_id value="1">
<tab_id value="351">
<tab_name value="test1"/>
<region_timezone value="1"/>
<registrationstatus value="2"/>
<eventstatus value="2"/>
<dist_activity value="5"/>
<dist_activity value="10068"/>
<dist_activity value="10070"/>
<dist_region value="5069"/>
<dist_region value="5069"/>
<dist_region value="5069"/>
</tab_id>
</product_id>
<product_id value="2">
<tab_id value="352">
<tab_name value="test2"/>
<region_timezone value="1"/>
<registrationstatus value="2"/>
<eventstatus value="2"/>
<dist_activity value="5"/>
<dist_activity value="10069"/>
<dist_activity value="10070"/>
<dist_region value="4457"/>
<dist_region value="7140"/>
<dist_region value="5069"/>
</tab_id>
</product_id>
</products>
这是我尝试:
<?php
$abc= $_POST['name'];
list($first,$second) = explode('in',$abc);
$text1[]=$first;
$text2[]=$second;
foreach($text1 as $event)
{
$event;
}
foreach($text2 as $region1)
{
$region1;
}
$r = file_get_contents('reg.xml');
$p = file_get_contents('product.xml');
$region = simplexml_load_string($r);
$product = simplexml_load_string($p);
list($entity) = $region->xpath("//*[@value='$event']/@entity_id");
$entity=(string)$entity;
echo "event:- $event, Region:- $region1, entity:- $entity";
?>
这是我的HTML文件:
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js">
</script>
<script>
$(function(){
$("#filter").click(function(){
var name = $('#select').val();
alert(name);
$.ajax({
type: "POST",
data: {"name":name} ,
url: "array.php",
async: false,
success: function(result) {
alert(result);
$("#result").text(result);
}
});
});
});
</script>
</head>
<body>
<input type="text" id="select">
<input type="button" id="filter" name="button" value="Search">
<div id="result">
</div>
</body>
</html>
我在文本框中输入该值:一天在Irungattukottai
结果
我已经试过这code,但它不工作,它不给我 $实体
。结果
如果我尝试手动设置 $事件
例如: - $事件
= 忠孝节义
值,那么工作结果
所以请帮助我这,我想我在我的的foreach
的错误。
谢谢你。
I input this value in the textbox:One Day in Irungattukottai
I've tried this code but it's not working and it doesn't give me $entity
.
If I try manually to set $event
eg:- $event
="One Day"
value then work
So help me out with this, I think my mistake in my foreach
.Thanks.
推荐答案
在foreach code看起来很奇怪 - 不知道你想要做那里。但我认为你有实际的问题是与爆炸(),因为它不看对在字两边的空间,让你的一天将结束与上月底Irungattukottai将空间必须在一开始的空间。
The foreach code looks really weird -- not sure what you're trying to do there. But I think the actual problem you have is with the explode(), because it doesn't look at the space on either side of the word 'in', so your One Day will end up with a space on the end and Irungattukottai will have a space at the start.
您可以修复通过在爆炸()或通过它产生的两个字符串使用TRIM()的空间。
You could fix that by including the spaces in the explode() or by using trim() on the two strings it produces.
这篇关于没有得到XPath中的数组值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!