Home>Article>Backend Development> How to use array_filter in php

How to use array_filter in php

(*-*)浩
(*-*)浩 Original
2019-09-05 10:53:04 2459browse

How to use array_filter in php

array_filterUse callback function to filter elements in the array

(PHP 4 >= 4.0.6, PHP 5, PHP 7) array_filter — Use a callback function to filter cells in an array

Pass each value in the input array to the callback function in turn. If the callback function returns TRUE, the current value of the input array will be included in the returned result array. The key names of the array remain unchanged.(Recommended learning:PHP video tutorial)

Return value: Return the filtered array

$arr = array( 
"1",
"2",
"3",
"4",
"5",
"",
"6",
"7",
"8",
false,
"14",
"15",
"195",
"",
"9",
);
//删除空字符串和false
$re1 = array_filter($arr);
var_dump($re1);
echo "------------------\n";
function add($num) {
$num = intval($num);
if($num >6) {
return true;
}
return false;
}
$res = array_filter($arr,"add");
var_dump($res);

Running result:

array(12) {
[0]=>
string(1) "1"
[1]=>
string(1) "2"
[2]=>
string(1) "3"
[3]=>
string(1) "4"
[4]=>
string(1) "5"
[6]=>
string(1) "6"
[7]=>
string(1) "7"
[8]=>
string(1) "8"
[10]=>
string(2) "14"
[11]=>
string(2) "15"
[12]=>
string(3) "195"
[14]=>
string(1) "9"
}
------------------
array(6) {
[7]=>
string(1) "7"
[8]=>
string(1) "8"
[10]=>
string(2) "14"
[11]=>
string(2) "15"
[12]=>
string(3) "195"
[14]=>
string(1) "9"

The above is the detailed content of How to use array_filter in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:What does array mean in php Next article:What does array mean in php