Blogger Information
Blog 11
fans 0
comment 0
visits 10126
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP关于数组筛选方法
小杂鱼
Original
1008 people have browsed it

for循环条件筛选偶数成员

  1. $arr = [22,23,15,14,11,19,18];
  2. // echo sizeof($arr) 获取数组长度;
  3. // echo count($arr) 获取数组长度;
  4. function odd(array $arr):array
  5. {
  6. $newArr = [];
  7. for ($i=0; $i < sizeof($arr); $i++) {
  8. if($arr[$i] % 2 == 0){
  9. $newArr[] = $arr[$i];
  10. }
  11. }
  12. return $newArr;
  13. }
  14. var_dump(odd($arr));
  15. //array(3) {[0]=>int(22)[1]=>int(14)[2]=>int(18) }

array_filter筛选奇偶数

  1. $arr = [6, 7, 8, 9, 10, 11, 12];
  2. // 返回输入整数是否为偶数 !($var & 1); &是二进制的写法
  3. // 返回输入整数是否为奇数数 $var & 1;
  4. $newArr = array_filter($arr, function($var){
  5. return !($var & 1);
  6. });
  7. var_dump(array_values($newArr));
  8. //array(4) {[0]=>int(6)[1]=>int(8)[2]=>int(10)[3]=>int(12) }
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post