0

我正在研究 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

有人可以帮助我的代码吗?

4

1 回答 1

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 ..

于 2012-09-09T11:09:39.870 回答