zoukankan      html  css  js  c++  java
  • PHP 数组过滤空值 array_filter

    array_filter

    • 使用回调函数过滤数组的元素,返回过滤后的数组。
    • 遍历 array 数组中的每个值,并将每个值传递给 callback 回调函数。 如果 callback 回调函数返回 true,则将 array 数组中的当前值返回到结果 array 数组中。数组键名保持不变。
    • 如果没有提供 callback 回调函数,将删除数组中 array 的所有“空”元素。
      // 不使用回调函数,可以过滤空值
      $entry = [
          0 => 'foo',
          1 => false,
          2 => -1,
          3 => null,
          4 => '',
          5 => '0',
          6 => 0,
      ];
      print_r(array_filter($entry));
       
      ## 返回结果
      Array
      (
          [0] => foo
          [2] => -1
      )
      
      
      
      // 使用回调函数 返回大于5的值
      $array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
      $return_array = array_filter($array, function ($value) {
          // 过滤小于等于5的值
          return $value > 5 ? true : false;
      });
      print_r($return_array);
       
      ## 返回结果
      Array
      (
          [5] => 6
          [6] => 7
          [7] => 8
          [8] => 9
          [9] => 10
      )
  • 相关阅读:
    Mybatis与Spring集成
    Mybatis 多对多
    Mybatis表关联多对一
    Mybatis表关联一对多
    Mybatis增删改查(CURD)
    Mybatis接口注解
    MyBatis环境配置及入门
    MyBatis教程
    Spring JDBC StoredProcedure类示例
    Spring JDBC SqlUpdate类示例
  • 原文地址:https://www.cnblogs.com/wangrongjie/p/15661706.html
Copyright © 2011-2022 走看看