How to check if all array item is more than N with array_reduce() in PHP

2 Answers

0 votes
$b = array_reduce([11, 13, 300, 98, 506, 17], function($yesno, $item){
        echo $yesno . ' : ' . $item . "<br />";
        return $yesno && $item > 10;
}, true); 

if ($b)
    echo "yes";
else
    echo "no";
 
  
/*
run:
   
1 : 11
1 : 13
1 : 300
1 : 98
1 : 506
1 : 17
yes 
   
*/

 



answered Oct 6, 2017 by avibootz
0 votes
$array = [11, 13, 300, 2, 506, 17];

$b = array_reduce($array, function($yesno, $item){
        echo $yesno . ' : ' . $item . "<br />";
        return $yesno && $item > 10;
}, true); 

if ($b)
    echo "yes";
else
    echo "no";
 
  
/*
run:
   
1 : 11
1 : 13
1 : 300
1 : 2
: 506
: 17
no 
   
*/

 



answered Oct 6, 2017 by avibootz

Related questions

1 answer 152 views
1 answer 169 views
1 answer 137 views
1 answer 170 views
1 answer 176 views
...