【PHP】php中的回调函数,匿名函数草拟
「代码」
<?php
$before = [
'user1' => 15,
'user2' => 3,
'user3' => 20,
'user4' => 8,
'user5' => 5
];
$result = check_array($before,function($v){
return $v % 3 === 0;
});
function check_array($before,$callback){
$after = [];
foreach ($before as $key => $v){
if($callback($v)){
$after[$key] = $v;
}
}
return $after;
}
var_dump($result);
结果 (jié guǒ)
array(2) {
["user1"]=>
int(15)
["user2"]=>
int(3)
}