日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP array_filter()函数与示例

發布時間:2025/3/11 php 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 notfunction 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()函数与示例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。