2012-09-09 65 views
0

我工作的一个XML和我使用的SimpleXML。PHP的SimpleXML数串

$xml = simplexml_load_file(a xml file); //this website is just chosen randomly 


    if(!in_array($iindex,$category_type)){ 
     $category_type[] = $iindex; 
     $category_type[$iindex] = 1; 
    } else { 
     $category_type[$iindex] = $category_type[$iindex] + 1; 
    } 
} 

foreach($category_type as $key=> $value){ 
    echo " number of $key is ". $value; 
} 

我得到的结果是目前

number of 0 is Really Funny Jokes 
number of Really Funny Jokes is 13 
number of 1 is Clean jokes 

我期待的

number of Really Funny Jokes is 13 
    number of Clean jokes is 6 
    number of Good jokes is 2 

可能有人用我的代码请帮助的结果呢?

回答

2
if(!array_key_exists($iindex, $category_type)){ 
//$category_type[] = $iindex; //**remove this line** 
$category_type[$iindex] = 1; 
} else { 

什么该行正在做的是它的插入在数组索引0,1,2 ..和值作为你的关键条目..

并使用array_key_exists ..

+0

没有,如果我删除该行,结果成为'真正搞笑笑话的数量是1 干净笑话的数量是1 好笑话的数量是1 孩子笑话的数量是1 动物笑话的数量是1 短小幽默笑话的数量是1' – olo

+0

那是因为你使用in_array,而你应该使用array_key_exists –

+0

You Rock!Rajat!很长时间以来一直在寻找解决方案 – olo