-3

有一个数组:

Array
(
    [Apple] => 1
    [Banana] => 2
    [Orange] => 1
    [Pie] => 3
)

我想检查一下,哪些数组值大于 1(重复等)并返回它们。

'Banana was found 2 times in the array, Pie even 3 times.
4

2 回答 2

2

您可以使用 array_filter 根据键/值过滤数组。它返回一个数组,其中仅包含与回调函数中的条件匹配的数组。

$greaterThanOne = array_filter($array, function($val){ return ($val > 1); });

foreach($greaterThanOne as $fruit=>$count){
    echo "$fruit was found $count times in the array.<br>";
}
于 2012-12-07T22:24:03.977 回答
0

你只需要迭代数组......

foreach ($array_with_fruits as $fruit=>$times){
    if ($times>1) { echo $fruit." was found ".$times." times"; }
}
于 2012-12-07T22:24:42.763 回答