在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
array_walk:array_walk — 使用用户自定义函数对数组中的每个元素做回调处理 <?php $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple"); function test_alter(&$item1, $key, $prefix) { $item1 = "$prefix: $item1"; } function test_print($item2, $key) { echo "$key. $item2<br />\n"; } echo "Before ...:\n"; array_walk($fruits, 'test_print'); array_walk($fruits, 'test_alter', 'fruit'); echo "... and after:\n"; array_walk($fruits, 'test_print'); ?> //运行结果 更多信息参考官方文档:http://php.net/manual/zh/function.array-walk.php array_maparray_map — 为数组的每个元素应用回调函数 Example #1: <?php function cube($n) { return($n * $n * $n); } $a = array(1, 2, 3, 4, 5); $b = array_map("cube", $a); print_r($b); ?>
Example #2: <?php $func = function($value) { return $value * 2; }; print_r(array_map($func, range(1, 5))); ?>
Example #3: <?php function show_Spanish($n, $m) { return("The number $n is called $m in Spanish"); } function map_Spanish($n, $m) { return(array($n => $m)); } $a = array(1, 2, 3, 4, 5); $b = array("uno", "dos", "tres", "cuatro", "cinco"); $c = array_map("show_Spanish", $a, $b); print_r($c); $d = array_map("map_Spanish", $a , $b); print_r($d); ?>
更多信息参考官方文档:http://php.net/manual/zh/function.array-map.php array_filter* array_filter — 用回调函数过滤数组中的单元* Example 1: <?php function odd($var) { // returns whether the input integer is odd return($var & 1); } function even($var) { // returns whether the input integer is even return(!($var & 1)); } $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); $array2 = array(6, 7, 8, 9, 10, 11, 12); echo "Odd :\n"; print_r(array_filter($array1, "odd")); echo "Even:\n"; print_r(array_filter($array2, "even")); ?>
Example #2: <?php $entry = array( 0 => 'foo', 1 => false, 2 => -1, 3 => null, 4 => '' ); print_r(array_filter($entry)); ?>
Example #3: <?php $arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4]; var_dump(array_filter($arr, function($k) { return $k == 'b'; }, ARRAY_FILTER_USE_KEY)); var_dump(array_filter($arr, function($v, $k) { return $k == 'b' || $v == 4; }, ARRAY_FILTER_USE_BOTH)); ?>
更多信息参考官方文档:http://php.net/manual/zh/function.array-filter.php 参考其他链接: https://www.ogeek.net/article/222535.htm 到此这篇关于php array_walk array_map array_filter区别案例详解的文章就介绍到这了,更多相关php array_walk array_map array_filter区别内容请搜索极客世界以前的文章或继续浏览下面的相关文章希望大家以后多多支持极客世界! |
2022-08-17
2022-11-06
2022-08-18
2022-07-18
2022-08-16
请发表评论