Ryan D Ryan D - 4 months ago 10
PHP Question

How can I iterate through multiple strings with multiple array values, in PHP?

* Updated question from revo's answer



Here is the working script with a better set of example strings to show my intent-

$strings[] = 'seventy five yards out';
$strings[] = 'sixty yards out';
$strings[] = 'one hundred fifty yards out';

$inputString = 'seventy two yards out';
$inputWords = str_word_count($inputString, 1);

$foundWords = [];

foreach ($strings as $key => $string) {
$stringWords = str_word_count($string, 1);
$wordsCount = array_count_values($stringWords);
$commonWords = array_intersect($inputWords, array_keys($wordsCount));
if (count($commonWords) > 0) {
foreach ($commonWords as $commonWord) {
$foundWords[$key][$commonWord] = $wordsCount[$commonWord];
}
}
}

print_r($foundWords);


How would I get it to print 'seventy five yards out' as it would be the actual closest to the text? I was thinking of dividing the word count to get a percentage but now think that might now work..

Answer

The key is to do a str_word_count() on each provided string separately. This way we are transforming into arrays and dealing with arrays are much simpler for what you desire.

array_count_values() counts values of an array which leads to having number of word occurrences.

$strings[] = 'seventy five yards out';
$strings[] = 'sixty yards out';
$strings[] = 'one hundred fifty yards out';

$inputString = 'seventy two yards out';
$inputWords = str_word_count($inputString, 1);

$probabilities = [];

foreach ($strings as $key => $string) {
    $stringWords = str_word_count($string, 1);
    $wordsCount = array_count_values($stringWords);

    $commonWords = array_intersect($inputWords, array_keys($wordsCount));

    if (count($commonWords) > 0) {
        foreach ($commonWords as $commonWord) {
            if (!isset($probabilities[$key])) $probabilities[$key] = 0;
            $probabilities[$key] += $wordsCount[$commonWord];
        }
        $probabilities[$key] /= count($stringWords);
    }
}
arsort($probabilities);
echo $strings[key($probabilities)];

Output:

seventy five yards out

Probabilities print_r($probabilities);:

Array
(
    [0] => 0.75
    [1] => 0.66666666666667
    [2] => 0.4
)
Comments