2012-09-09 3 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, ...과 값을 배열에 항목을 삽입하는 것입니다 ..

그리고 array_key_exists을 사용 ..

+0

아니, 경우를 나는 결과가 정말 재미 농담의 수는 클린 농담의 1 수는 좋은 농담 1 수는 아이의 수는 농담 한 동물 농담의 1 수 짧은 유머 농담의 1 개 번호입니다입니다입니다'가, 그 줄을 제거 1'은 – olo

+0

입니다. 왜냐하면 ur은 in_array를 사용하기 때문에, u는 array_key_exists를 사용해야합니다. –

+0

You Rock! Rajat! 꽤 오랜 시간 동안 해결책을 찾고 있었다. – olo