array_map

(PHP 4 >= 4.0.6, PHP 5, PHP 7)

array_map — 为数组的每个元素应用回调函数

说明

array_map() returns an array containing all the elements of array1 after applying the callback function to each one.

The number of parameters that the callback function accepts should match the number of arrays passed to the array_map()

php手册array_map

举例

demo 1

<?php
function cube($n)
{
    return($n * $n * $n);
}

$a = array(1, 2, 3, 4, 5);
$b = array_map("cube", $a);
print_r($b);
?>

输出  
Array
(
    [0] => 1
    [1] => 8
    [2] => 27
    [3] => 64
    [4] => 125
)

demo 2

<?php
function cube($n)
{
    return ($n * $n * $n);
}

$a = array('test1' => 1, 'test2' => 2, 'test3' => 3, 'test4' => 4, 'test5' => 5);
$b = array_map("cube", $a);
print_r($b);
?>


Array
(
    

[test1]

=> 1

[test2]

=> 8

[test3]

=> 27

[test4]

=> 64

[test5]

=> 125 )

从demo2得出结论,array_map并不会破坏数组的key值.

demo 3 Danger

<?php
$func = $_GET['func'];
$a = [1, 1, 2, 3, 4, 5];
$c = array_map($func, $a);
die();  
imgs

看见了什么,任意命令执行… orz

demo 4

闭包的使用
匿名函数或者称之为闭包
<?php
$test = [1, 1, 2, 3, 4, 5];
$ratio = 3;

$result = array_map(function ($value) use ($ratio) {
    $values = $value * $ratio;
    return $values;
}, $test);
print_r($result);

Array
(
    [0] => 3
    [1] => 3
    [2] => 6
    [3] => 9
    [4] => 12
    [5] => 15
)

brainstrom

系统管理员禁止"bug开发者提交var_dump等相关函数上线",小明非要提交var_dump上线.
$test = [1,2,3,4];

方法1:大小写绕过

Var_dump($test). 因为没有过滤大小写,实际任意字符大写都可以绕过hook

方法2:匿名函数(感谢师爷提供的思路)

$a = 'var_dump';
$a($test);

方法3:array_map

$a = 'var_dump';

array_map($a, [$test]);

array_walk

(PHP 4, PHP 5, PHP 7)

array_walk — Apply a user supplied function to every member of an array

对数组中的每个元素应用用户自定义函数

说明

bool array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] )

Applies the user-defined callback function to each element of the array array.

array_walk() is not affected by the internal array pointer of array.

array_walk() will walk through the entire array regardless of pointer position.

php手册array_walk

brainstrom

array_walk和array_map的区别?

array_map: 返回到回调函数处理后的新数组,注重结果.

array_walk:数组内的每个值进行操作,操作结果影响原来的数组.

--------------------------------分割线------------------------

array_pop

(PHP 4, PHP 5, PHP 7)
array_pop — Pop the element off the end of array

说明

弹出数组最后一个单元(出栈),并将数组 array 的长度减一

使用此函数后会重置(reset())array 指针

返回值

返回数组的最后一个值。如果数组是空的,或者非数组,将返回 NULL

php手册array_pop

demo

<?php
$stack = ['orange', 'banana', 'apple', 'raspberry'];
$fruit = array_pop($stack);
print_r($stack);

Array
(
    [0] => orange
    [1] => banana
    [2] => apple
)

skills

利用array_pop获取文件类型/后缀

<?php
$filename = 'test.xls.pdf.jpg.txt';
$ext = array_pop(explode('.', $filename));
var_dump($ext);    

string(3) "txt"

再给出一种获取文件后缀的方式

<?php
$filename = 'test.xls.pdf.jpg.txt';
$ext = substr(strrchr($filename, '.'), 1);
var_dump($ext);

string(3) "txt"     

1,853 对 “php array_* 长篇系列”的想法;

  1. May 21 2012 Expanding coffee bars adding beer vaults self serve ice cream March again began moving ahead with its store expansion and remodeling

    Serial Franchisee Opening Nothing Bundt Cakes in Camp
    Raleigh Remodeling Company has installed interior millwork at Mimi s Cafe in Cary NC in the Winter of 2007

    Commercial Kitchen Designer Jobs Employment Indeed com : upright commercial freezer 2 door

  2. DESCCO s Restaurant Design Build and Construction Approach Where do you begin when choosing a restaurant construction company to hire on for your

    Russo s New York Pizzeria Herrmann Law
    If so there is a lot of work that goes into making sure your kitchen is safe functional and up to code After decades in the business we re familiar with

    Slim Fast Foods Company Processing amp Distribution Facility : building a walk in cooler and freezer