无法匹配simplexml对象和变量?
问题描述:
我有一个PHP文件,其中一个xml文件通过simplexml加载和搜索相应的ID,一切工作正常,但是当与给定的ID匹配,它不工作,如果我回显他们两个,他们显示但如果我要是用语句来匹配他们不会匹配无法匹配simplexml对象和变量?
这里是我的PHP代码
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$cat_xml= simplexml_load_file('http://artstack.com/sites/77/site_categories.xml');
$product_xml= simplexml_load_file('http://artstack.com/sites/77/site_products.xml');
function getcatid($product_name,$product_xml){
foreach($product_xml as $product){
if($product){
if($product_name==$product->url){
return $product->cat_id;
}
}
}
}
function getcat($cat_id,$cat_xml){
foreach($cat_xml as $cat){
if($cat){
if($cat_id==$cat->id){
echo $cat['name'];
}
else{
if(isset($cat->category)){
//sub cat
foreach($cat->category as $subcat){
if($subcat){
echo $cat_id." > ".$subcat->id."<br>";
if($cat_id==$subcat->id){
echo $cat_id." > ".$subcat->id."<br>";
exit;
}
}
}
}
}
}
}
}
getcat(getcatid($_GET['pro'],$product_xml),$cat_xml);
?>
输出是
75 > 73
75 > 74
75 > 75
但它必须是
75 > 73
75 > 74
75 > 75
75 > 75
答
它的工作我用的类型转换后
if(intval($cat_id)==intval($subcat->id)){
THANKS
+0
事实上,simplexml的返回simplexmlobject,当回声'd显示正常,但不是一个字符串,因此失败了字符串比较(或int比较)。 webbandit的提示会让你获得相同的信息。 – mseancole 2012-04-06 13:09:47
尝试使用'的var_dump()',而不是'echo'看到变量转储 – 2012-04-06 12:56:55