Blogger Information
Blog 55
fans 0
comment 0
visits 50431
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP数组元素的回调处理-0824
Bean_sproul
Original
729 people have browsed it

array_walk():对数组中每个元素的键和值进行操作

array_filter():回调处理数组中的每个元素的值,仅返回结果为true的元素

实例

<?php

echo "<h1>数组的回调</h1>";

/* 将一个函数做为参数进行传递
 1. array_filter($arr, $callback)
 2. array_walk($arr, $callback())
 */

//1. array_filter():回调处理数组中的每个元素的值,仅返回结果为true的元素
$arr1 = [1,2,'',3,null,88,false,'php'];
echo '<pre>';
print_r($arr1);
echo '<hr>';
echo var_export($arr1,true),',共有:',count($arr1),'个元素<hr>';

// ''空字符串,0,null,false: false 非常适合删除数组中的空元素
$arr2 = array_filter($arr1);//过滤掉;
echo '新数组',var_export($arr2,'ture'),',共有:',count($arr2),'个元素<hr>';

//传入一个回调: 匿名函数
$arr3 = ['html','css','js'];

$arr4 = array_filter($arr3,function ($value){
   return $value !== 'css';
});
echo var_export($arr4,'ture');
echo "<hr>";
//2.array_walk():对数组中每个元素的键和值进行操作
$arr5 = ['id' =>1,'name' => 'admin', 'email' =>'admin@admin' ];
echo var_export($arr5,'ture'),'<hr>';
echo "<pre>";
array_walk($arr5, function($value,$key){
echo $key,':',$value,'<br>';
});
echo '<hr>';
//回调的第三个参数的用法

array_walk($arr5, function(&$value,$key,$name){
if ($value != $name) {
        exit('无权查看');
    } else {
        exit($key.':'.$value);
    }

},'admin1');

?>

运行实例 »

点击 "运行实例" 按钮查看在线实例

Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post