PHP array_filter()函数 (PHP array_filter() Function)

array_filter() function is used to apply a filter on array elements based on the function and returns the array with filtered elements, it accepts an array to be checked and a callback function. The callback function is used to validate the elements in the array.

array_filter()函数用于基于该函数在数组元素上应用过滤器,并返回带有已过滤元素的数组,它接受要检查的数组和回调函数。 回调函数用于验证数组中的元素。

Syntax:

句法:

    array_filter(array,callback_function) : array

Here,

这里,

  • array is the input array in which we have to apply the filter.

    数组是我们必须在其中应用过滤器的输入数组。

  • callback_function is the function, in which we write the condition to be validated.

    callback_function是函数,我们在其中编写要验证的条件。

Examples:

例子:

    Input:
$arr = array(10, 20, -10, -20, 50, 0);
//here we have to filter the positive numbers
//the callback function to check the positive number is "isPositive()"
Function calling:
$temp = array_filter($arr, "isPositive");
Output:
Array
(
[0] => 10
[1] => 20
[4] => 50
)
So, here 0th , 1st and 4th elements are positive

PHP code 1: Find the positive number from given array of the numbers.

PHP代码1:从给定的数字数组中找到正数。

<?php
//function to check wheather number is positive or not
function isPositive($val)
{
if($val>0)
return $val;
}
// array
$arr = array(10, 20, -10, -20, 50, 0);
// array with only positive value
$temp = array_filter($arr, "isPositive");
print_r ($temp);
?>

Output

输出量

Array
(
[0] => 10
[1] => 20
[4] => 50
)
.minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } } .minHeight{ min-height: 250px; } @media (min-width: 1025px){ .minHeight{ min-height: 90px; } }

PHP code 2: Find the persons who are eligible for voting from given array of persons

PHP代码2:从给定的一组人员中找到有资格投票的人员

Here, we also have the "keys" and based on the age key we are checking the voting eligibility.

在这里,我们还有“键”,并且根据年龄键,我们正在检查投票资格。

<?php
//function to check wheather person is eligible
//for voting or not?
function isVoter($val)
{
if($val['age']>=18)
return $val;
}
// person's array
$arr = array(
array("name" => "Prem", "age" => 28,"city" => "Gwalior",),
array("name" => "Manju", "age" => 25,"city" => "Gwalior",),
array("name" => "Radib Kar", "age" => 23,"city" => "Chennai",),
array("name" => "Prerana", "age" => 17,"city" => "Gwalior",),
);
// array with voting eligible persons
$temp = array_filter($arr, "isVoter");
print_r ($temp);
?>

Output

输出量

Array
(
[0] => Array
(
[name] => Prem
[age] => 28
[city] => Gwalior
)
[1] => Array
(
[name] => Manju
[age] => 25
[city] => Gwalior
)
[2] => Array
(
[name] => Radib Kar
[age] => 23
[city] => Chennai
)
)

翻译自: https://www.includehelp.com/php/array_filter-function-with-example.aspx

PHP array_filter()函数与示例相关推荐

  1. php指定长度 分割整形,php指定长度分割字符串str_split函数用法示例

    本文实例讲述了php指定长度分割字符串str_split函数用法.分享给大家供大家参考,具体如下: 示例1:$str = 'abcdefgh'; $arr = str_split($str,2); 运 ...

  2. getdate函数_PHP getdate()函数与示例

    getdate函数 PHP getdate()函数 (PHP getdate() function) getdate() function is used to get the local date/ ...

  3. 安卓log.e函数打印示例_log1p()函数以及C ++中的示例

    安卓log.e函数打印示例 C ++ log1p()函数 (C++ log1p() function) log1p() function is a library function of cmath ...

  4. gettimeofday_PHP gettimeofday()函数与示例

    gettimeofday PHP gettimeofday()函数 (PHP gettimeofday() function) gettimeofday() function is used to g ...

  5. dir函数_PHP dir()函数与示例

    dir函数 PHP dir()函数 (PHP dir() function) dir() function is an instance of the directory class, it is u ...

  6. PHP rewinddir()函数与示例

    PHP rewinddir()函数 (PHP rewinddir() function) rewinddir() function is used to rewind/reset the direct ...

  7. stl swap函数_C ++ STL | vector :: swap()函数与示例

    stl swap函数 C ++ STL vector :: swap()函数 (C++ STL vector::swap() function) vector::swap() function is ...

  8. C#回调函数应用示例,形象比喻方法助理解

    C#回调函数应用示例,形象比喻方法助理解,整理了一个简单的例子来说明回调函数的用法: namespace CallBackFunction {     class Program     {      ...

  9. php解escape,PHP下escape解码函数用法示例

    这篇文章主要为大家详细介绍了PHP下escape解码函数用法示例,具有一定的参考价值,可以用来参考一下. 感兴趣的小伙伴,下面一起跟随512笔记的小编小韵来看看吧!GB2312编码: 代码如下: fu ...

最新文章

  1. 北京联通限制上传到电信网络服务器
  2. 计算机之间是如何实现通信的?—Vecloud微云
  3. Linux下防火墙开放端口
  4. 利用Cobbler批量快速网络安装CentOS
  5. KKT条件和拉格朗日乘子法
  6. Redis站点流量统计HyperLogLog
  7. WSGI服务器实践二--实践一个基本功能的WSGI服务器
  8. C语言之32个关键字
  9. 安全基础知识 最强0到33600端口详解
  10. 导入AIDL例子的时候,在asBinder出现错误
  11. 寻找中项和第k小元素c语言,寻找一个序列中第k小的元素——分治法
  12. Rust : 危险! 关于字符串切片以及取值......
  13. 计算机网络哈勃,NASA已基本确认哈勃故障原因 出在电源控制单元上
  14. 流程框图-各方框含义
  15. 北京内推 | 微软亚洲研究院自然语言计算组招聘NLP研究实习生
  16. 【线性代数笔记】正定矩阵及其性质
  17. MARKDOWN--介绍http://www.jianshu.com/p/q81RER
  18. broforce怎呢上机器人_杀戮尖塔机器人怎么玩 机器人卡组教学通关攻略
  19. 计算机磁盘怎么更改,如何对电脑中的磁盘盘符号进行修改?
  20. NFT市场如何拆分Opensea?

热门文章

  1. docker mysql57_docker安装mysql57
  2. 使用百度云智能SDK和树莓派搭建简易的人脸识别系统 Python语言版
  3. 华立学院计算机组成原理考试,广东工业大学华立学院计算机组成原理期末复习重点...
  4. 为什么计算机语言都是英语,编程为什么都是英语 编程为什么没有汉语
  5. Xtrabackup备份与恢复
  6. 锁底层之内存屏障与原语指令
  7. 从0到1使用VUE-CLI3开发实战(五):模块化VUEX及使用vuetify
  8. JavaMail(四):接收邮件
  9. 《Android 应用测试指南》——第2章,第2.4节包浏览器
  10. 用于检测敏感词的 PHP 扩展