PHP simplexml计数字符串


PHP simplexml count string

我正在研究一个XML,我使用了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

有人能帮我查一下密码吗?

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

这行所做的是在数组中插入一个索引为0,1,2..的条目,值为关键字。。

并使用array_key_exists。。