Eric Evans
Eric Evans

Reputation: 670

Find occurrences of length of words in array

I'm trying to find the number of occurrences length in array;

Here's what I've tried, but I don't know what to do after.

function instances_and_count($input) {
    $arr = explode(' ', $input);
    $count = 0;
    $test = [];
    foreach ($arr as $arr_str) {
        if ($count != 0) {
            $test = [$count => 'hi'];
            print_r($test);
        }
        $count++;
    }
}

$test = 'A heard you say something I know you ain\'t trying to take me homeboy.';
instances_and_count($test);

In this example, I explode a string to make an array. I need a count of let's say all words with a length of 1 which in this string it's a count of 2 (A and I); How can I do this for all lengths?

Upvotes: 0

Views: 56

Answers (2)

Nick
Nick

Reputation: 147166

PHP's array functions are really useful here; we can convert our exploded array of strings to an array of string lengths using array_map and strlen, and then use array_count_values to count how many words of each length there are:

$test = 'A heard you say something I know you ain\'t trying to take me homeboy.';

$counts = array_count_values(array_map('strlen', explode(' ', $test)));
print_r($counts);

Output:

Array
(
    [1] => 2
    [5] => 2
    [3] => 3
    [9] => 1
    [4] => 2
    [6] => 1
    [2] => 2
    [8] => 1
)

Demo on 3v4l.org

Note that there is a length of 8 in this array for the "word" homeboy. This can be avoided either by stripping trailing punctuation from the string, or (better) using str_word_count to extract only whole words from the original string. For example (thanks @mickmackusa):

$test = 'I heard you say something I know you ain\'t trying to take me homeboy.';

$counts = array_count_values(array_map('strlen', str_word_count($test, 1)));
print_r($counts);

Output:

Array
(
    [1] => 2
    [5] => 2
    [3] => 3
    [9] => 1
    [4] => 2
    [6] => 1
    [2] => 2
    [7] => 1
)

Demo on 3v4l.org

If you want to output the array with keys in order, just use ksort on it first:

ksort($counts);
print_r($counts);

Output:

Array
(
    [1] => 2
    [2] => 2
    [3] => 3
    [4] => 2
    [5] => 2
    [6] => 1
    [8] => 1
    [9] => 1
)

This is not necessary for use within your application.

Upvotes: 2

04FS
04FS

Reputation: 5820

Use the length of the word as array key. For each word you are looping over, check if an array entry for that length already exists - if so, increase the value by one, otherwise initialize it with 1 at that point:

function instances_and_count($input) {
  $words = explode(' ', $input);
  $wordLengthCount = [];
  foreach($words as $word) {
    $length = strlen($word);
    if(isset($wordLengthCount[$length])) {
      $wordLengthCount[$length] += 1;
    }
    else {
      $wordLengthCount[$length] = 1;
    }
  }
  ksort($wordLengthCount);
  return $wordLengthCount;
}

Result:

array (size=8)
  1 => int 2
  2 => int 2
  3 => int 3
  4 => int 2
  5 => int 2
  6 => int 1
  8 => int 1
  9 => int 1

Upvotes: 2

Related Questions