4

I am using Codeigniter framework of PHP and trying to extract keywords from the page. The complete code for reference can be seen here. It is not ready-made though.

The issue is due to the array function in the following line:

$keywordCounts = array_count_values( $words );

The error message being displayed is as follows:

A PHP Error was encountered

Severity: Warning

Message: array_count_values() [function.array-count-values]: Can only count STRING and INTEGER values!

EDITED: The array $words for reference can be found here.

There are no special symbols or invalid characters to my knowledge in the $words array. Hyphens and periods are not read by the function or is there some other issue ?

10
  • Are you passing a string into the function or an array?! Commented Jan 17, 2013 at 9:04
  • Its an array called $words which I am passing as you can see. I have displayed that array as a string of words separated by hyphens for reference. So that someone may find some word which the function cannot read either as a string or an integer. Commented Jan 17, 2013 at 9:07
  • can you do a formatted output of your $words array as you can see below there is no error when passing your hyphenated list of words exploded into an array to the function. Commented Jan 17, 2013 at 9:10
  • 1
    Some values in your array seems to be null and null is neither string nor integer: codepad.viper-7.com/0bniV2 Commented Jan 17, 2013 at 11:12
  • 1
    @RahulYadav Forgive me, I eliminated not null values. Here's what you need: codepad.viper-7.com/RuQLGw Commented Jan 17, 2013 at 11:59

1 Answer 1

15

you have null values in your array. you have to replace them before working with array_count_values like this:

$x = array('s'=>'ss', 'a',4 , 'sss' => null);

$ar = array_replace($x,array_fill_keys(array_keys($x, null),''));

$v = array_count_values($ar);

var_dump($v);

which will result:

array (size=4)
  'ss' => int 1
  'a' => int 1
  4 => int 1
  '' => int 1
Sign up to request clarification or add additional context in comments.

2 Comments

Okay this works! Is there any way to remove the null entry from the array ? And only the first and last entries of array are null. Is this happening due to some other reason or inconsistency ?
it depends on your code, you haven't posted much code to see why it's behaving like this. and yes, you can delete the null valued indices of your array, take a look here and here too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.