olo
olo

Reputation: 5271

PHP simplexml count string

I am working on a XML and I used 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;
}

the result I got currently is

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

The result I am expecting is

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

Could someone help with my code please?

Upvotes: 0

Views: 95

Answers (1)

Rajat Singhal
Rajat Singhal

Reputation: 11264

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

what that line is doing is it's inserting an entry in array with index 0,1,2.. and value as your key..

And use array_key_exists..

Upvotes: 2

Related Questions