如何在PHP中输出具有四个或更多字母的数组中的元素,并说明它们出现的次数

How do I output the words with four or more letters in them from the array and state how many times they occur?

$selection = array("house", "are", "better", "love", "dog",
    "love", "don't", "me", "like", "apples", "frank", "better", "you", 
    "like", "house", "better", "love", "cream");
$array2 = array();
foreach ($selection as $word) {
    if (strlen($word) >= 4) {
        $array2[] = $word;
        print_r(array_count_values($word));
    }
}
return $array2;
foreach ($array2 as $key => $value) {
    printf("The word '$key' appears $value times<br>
");
}

There are several problems in your code. You should use the word as the array key, and maintain a running count.

Also, you are calling return, which effectively stops the script.

Finally, use more descriptive variable names to help keep track of what you're doing.

<?php
$selection = array("house", "are", "better", "love", "dog",
                   "love", "don't", "me", "like", "apples", "frank", "better", "you", 
                   "like", "house", "better", "love", "cream");
$results = array();
foreach ($selection as $word) {
    if (strlen($word) >= 4) {
        if (array_key_exists($word, $results) == false)
            $results[$word] = 0;
        $results[$word]++;
    }
}
foreach ($results as $word=>$count) {
    print "The word '$word' appears $count times<br>
";
}
?>

http://codepad.viper-7.com/pq89ic

Documentation & Related Reading

Do This:

<?
$selection = array("house", "are", "better", "love", "dog",
    "love", "don't", "me", "like", "apples", "frank", "better", "you", 
    "like", "house", "better", "love", "cream");
$array2 = array();
foreach ($selection as $word) {
    if (strlen($word) >= 4) {
        if (isset($array2[$word])) $array2[$word]++;
            else $array2[$word] = 1;
    }
}
foreach ($array2 as $key => $value) {
    printf("The word '$key' appears $value times<br>
");
}
?>

SEE WORKING CODE