PHP array_filter()函數與示例

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
)

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

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/541291.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/541291.shtml
英文地址,請注明出處:http://en.pswp.cn/news/541291.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

微軟宣布以 262 億美元現金收購 LinkedIn

北京時間6月13日20:45&#xff08;美國當地時間6月13日上午8:45&#xff09;&#xff0c;微軟(Nasdaq: MSFT)和LinkedIn(領英&#xff0c;NYSE: LNKD)對外宣布&#xff0c;雙方達成協議&#xff0c;微軟宣布將以每股196美元、總價262億美元的價格收購LinkedIn&#xff0c;由現金…

linux vim 快速定位位置,vim快速移動定位的一些操作命令

很多時候你在編輯文件時移動光標都會花很長時間&#xff0c;這樣就無法高效的編輯了&#xff0c;要想達到高效編輯最好能夠將光標馬上定位到你需要的位置&#xff0c;vim就是可以滿足你這樣需求的一款編輯器&#xff0c;本節就要講一下vim快速移動定位的一些操作命令。下面的操…

本月初 本月末 java_本月內容作家(2018年8月)

本月初 本月末 javaWe are feeling glad to announce that Prerana Jain is the Content Writer Of The Month (August 2018) on the basis of her excellent contribution and her support to us. 我們很高興地宣布&#xff0c; Prera??na Jain憑借其出色的貢獻和對我們的支…

“玲瓏杯”線上賽 Round #15 河南專場 F 咸魚文章

elttiL moT nwod eht teerts sllac ruo god ” ehT peek god ” . piZ si a peehs god . tuB nehw moT seirt ot yas ” peeS ” , ti semoc tuo ” peek ” . dnA ni a yaw moT si thgir . piZ si syawla gnignirb sgniht oh rof su ot peek ! ll’I llet uoy tuoba emos fo m…

線性代數向量內積_向量的外積| 使用Python的線性代數

線性代數向量內積Prerequisites: 先決條件&#xff1a; Defining Vector using Numpy 使用Numpy定義向量 Learn: how to code for outer product of two vectors using am inbuilt function in numpy library? 了解&#xff1a;如何使用numpy庫中的內置函數為兩個向量的外部乘…

c語言案例朗讀工具源碼,C語言編寫簡單朗讀小工具(有源碼)

原標題&#xff1a;C語言編寫簡單朗讀小工具(有源碼)最近不少人在后臺留言說學C都是面對枯燥的控制臺程序&#xff0c;能不能體現一下C語言的實際用途&#xff0c;今天我們就理論結合實踐一把&#xff1a;C語言結合VBS腳本編寫一個簡單的朗讀小工具&#xff0c;做一個能夠發音的…

WAS集群系列(5):集群搭建:步驟3:安裝IHS軟件

選擇“安裝IBM HTTPServer”選項&#xff0c;點擊“安裝向導”。例如以下圖提示&#xff1a; 安裝提示&#xff0c;逐步點擊“下一步”&#xff0c;當中偶有幾處細節注意就可以。列舉例如以下&#xff1a; &#xff08;1&#xff09;、產品安裝路徑與先前WAS軟件所安裝路徑統一…

L1-056 猜數字 C語言,PAT L1-032 Left-pad

根據新浪微博上的消息&#xff0c;有一位開發者不滿NPM(Node Package Manager)的做法&#xff0c;收回了自己的開源代碼&#xff0c;其中包括一個叫left-pad的模塊&#xff0c;就是這個模塊把javascript里面的React/Babel干癱瘓了。這是個什么樣的模塊&#xff1f;就是在字符串…

單鏈表刪除整表_單鏈表刪除

單鏈表刪除整表Deletion can be at various positions like: 刪除可以在各個位置進行&#xff0c;例如&#xff1a; Deleting the first node 刪除第一個節點 Deleting the last node 刪除最后一個節點 Deleting the intermediate node 刪除中間節點 刪除單個鏈表中的第一個節…

WordPress數據表wp-options數據字段存JSON數據

2019獨角獸企業重金招聘Python工程師標準>>> wp_options表是WordPress中最重要的表&#xff0c;一切程序設置、主題設置和絕大多數插件的設置大都保存在此表。 WordPress里面數據表wp-options數據字段存JSON數據 &#xff1a; a:90: {s:11:"^wp-json/?$"…

css scroll屬性_CSS中的scroll-behavior屬性

css scroll屬性CSS | 滾動行為屬性 (CSS | scroll-behavior property) Who does not want their links to function smoothly and attractively? This type of functionality is very easy to implement. All you need is a bit of awareness about the property that would h…

c 語言五子棋游戲代碼,C語言案例:控制臺版本的五子棋游戲【代碼】

該樓層疑似違規已被系統折疊 隱藏此樓查看此樓return true;}x;}return false;}bool isLeftInclinedWin(bool isBlack,int x,int y){char c isBlack ? :O;int count;while(x>0 && y>0 && state[x][y] c){y--;x--;}count 0;if(state[x][y] c) count 1…

基于HTML5 Canvas 實現彈出框

用戶鼠標移入時&#xff0c;有彈出框出現&#xff0c;這樣的需求很常見。這在處理HTML元素實現時簡單&#xff0c;但是如果是對 HTML5 Canvas 構成的圖形進行處理&#xff0c;這種方法不再適用&#xff0c;因為 Canvas 使用的是另外一套機制&#xff0c;無論在 Canvas 上繪制多…

半素數c語言,非常簡單的c題目 不懂 緊急求助

1 半素數素數是指大于1且只有1和他本身兩個因子的正整數&#xff0c;例如2、3、5、89都是素數&#xff0c;2、4、10都不是素數。在這里我給半素數下一個定義&#xff1a;一個大于1的正整數能分解為兩個素數的乘積&#xff0c;那么這個正整數就是半素數&#xff0c;比如62*3&am…

JavaScript | 嵌套if的示例

Example: 例&#xff1a; In this example, we are reading salary of an employee and finding the discount and net pay based on given salary and discount rate. 在此示例中&#xff0c;我們正在讀取員工的薪水&#xff0c;并根據給定的薪水和折扣率找到折扣和凈工資。 …

POJ 1014 Dividing 背包

二進制優化&#xff0c;事實上是物體的分解問題。 就是比方一個物體有數量限制&#xff0c;比方是13&#xff0c;那么就須要把這個物體分解為1。 2&#xff0c; 4&#xff0c; 6 假設這個物體有數量為25&#xff0c;那么就分解為1&#xff0c; 2&#xff0c; 4。 8。 10 看出規…

node.js 中間件_Node.js中的Passport中間件(模塊)

node.js 中間件Hi! Welcome to Node.js Authentication Series, where well study and program the passport module or middleware. 嗨&#xff01; 歡迎使用Node.js身份驗證系列 &#xff0c;我們將在其中研究和編程通行證模塊或中間件 。 Nowadays, an important tool in m…

android開發自動提示框,Android 多種簡單的彈出框樣式設置代碼

簡介這是一個基于AlertDialog和Dialog這兩個類封裝的多種彈出框樣式&#xff0c;其中提供各種簡單樣式的彈出框使用說明。同時也可自定義彈出框。項目地址&#xff1a;http://www.easck.com/jjdxmashl/jjdxm_dialogui特性1.使用鏈式開發代碼簡潔明了2.所有的彈出框樣式都在Dial…

小程序中利用Moment.js格式時間

2019獨角獸企業重金招聘Python工程師標準>>> LeanCloud給的日期是ISO格式&#xff0c;比如2017-06-05T14:08:20.589Z&#xff0c;直接顯示在頁面上體驗不好。 凡是有關日期的&#xff0c;格式化、計算&#xff0c;用moment就夠了。 1.下載 http://momentjs.com/ 選m…

php 檢查數組為空_檢查數組是否為空在PHP中

php 檢查數組為空Given an array and we have to check if array is an empty or not using PHP. 給定一個數組&#xff0c;我們必須檢查數組是否為空或不使用PHP。 To check whether an array is empty or not, we can use a built-in function empty(), in other cases wher…