在php输出

时间:2016-06-20 19:33:35

标签: php

我试图在下面的单独输出中选择并仅显示偶数           

 function toms($c,$first = 0,$second = 1)
 {
     $toms = [$first,$second];
     for($i=1;$i<$c;$i++)
     {
         $toms[] = $toms[$i]+$toms[$i-1];
     }
     return $toms;
 }
 echo "<pre>";
 print_r(toms(33));
 ?>

目前此输出

 array
 (
     [0] => 0
     [1] => 1
     [2] => 1
     [3] => 2
     [4] => 3
     [5] => 5
     [6] => 8
     [7] => 13
     [8] => 21
      [9] => 34
     [10] => 55
     [11] => 89
     [12] => 144
     [13] => 233
     [14] => 377
     [15] => 610
     [16] => 987
     [17] => 1597
     [18] => 2584
     [19] => 4181
     [20] => 6765
     [21] => 10946
     [22] => 17711
     [23] => 28657
     [24] => 46368
     [25] => 75025
     [26] => 121393
     [27] => 196418
     [28] => 317811
     [29] => 514229
     [30] => 832040
     [31] => 1346269
     [32] => 2178309
     [33] => 3524578
)

任何人都知道如何只显示返回的偶数,所以我希望有2个,8个,34个等等

谢谢

1 个答案:

答案 0 :(得分:4)

您可以使用array_filter

sudo /etc/init.d/mysql restart

或者,如果您想过滤掉print_r(array_filter(toms(33), function($number){ return $number % 2 == 0; }));

0

更具可读性:

print_r(array_filter(toms(33), function($number){
    return $number != 0 && $number % 2 == 0;
}));