Blogger Information
Blog 20
fans 0
comment 0
visits 14896
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
数组元素的回调处理 2018.8.24
李逍遥
Original
563 people have browsed it

实例

<?php
// 数组的回调处理

//1. array_filter() 回调处理数组中的每个元素的值,仅返回结果为true的元素
// 适合删除数组中的空元素
echo '<pre>';
echo 'array_filter() <br>';
$a = ['','0',null,false,1,'abc'];
$a = array_filter($a);
echo var_export($a),'<hr>';

//传入一个回调:匿名函数
$language = ['PHP','HTML','PYTHON','JAVA','C++'];
$b = array_filter($language,function ($value){
    return $value !== 'HTML';
});
echo var_export($b);

//2. array_walk()  对数组中每个元素的键和值进行处理
echo 'array_walk() <hr>';
$type = ['integer'=>'整数型','float'=>'浮点型','string'=>'字符串型','boolean'=>'布尔型','array'=>'数组型','object'=>'对象型'];
array_walk($type,function ($value,$key){
    echo $key,' ==> ',$value,'<br>';
});
echo '<hr>';

//回调的第三个参数的用法

$type = ['integer'=>'整数型','float'=>'浮点型','string'=>'字符串型','boolean'=>'布尔型','array'=>'数组型','object'=>'对象型'];
array_walk($type,function ($value,$key,$integer){
    if ($value !== $integer){
        exit('无权查看');
    }else{
        exit($key.$value);
    }
},'整数型');
// 问:回调第三个参数只能对数组的第一个元素有效?

运行实例 »

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


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