2012-09-09 113 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