php 開發(fā)經(jīng)常遇到數(shù)組處理,會涉及到題目中這幾個函數(shù)。這個函數(shù)功能類似,很多時候容易混淆。
array_walk:
array_walk — 使用用戶自定義函數(shù)對數(shù)組中的每個元素做回調(diào)處理
1. 用戶自定義的函數(shù)處理每一個元素
2. 直接修改原數(shù)組,不會創(chuàng)建新的數(shù)組
3. 可以傳遞額外的參數(shù)
Example #1 :
?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. $item2br />\n";
}
echo "Before ...:\n";
array_walk($fruits, 'test_print');
array_walk($fruits, 'test_alter', 'fruit');
echo "... and after:\n";
array_walk($fruits, 'test_print');
?>
//運行結(jié)果
Before ...:
d. lemon
a. orange
b. banana
c. apple ... and after:
d. fruit: lemon
a. fruit: orange
b. fruit: banana
c. fruit: apple
更多信息參考官方文檔:http://php.net/manual/zh/function.array-walk.php
array_map
array_map — 為數(shù)組的每個元素應用回調(diào)函數(shù)
1. 回調(diào)函數(shù),應用到每個數(shù)組里的每個元素
2. 可以處理多個數(shù)組,合并處理
3. 返回新建的數(shù)組
4. 傳入 NULL 作為回調(diào)函數(shù)的名稱,將創(chuàng)建多維數(shù)組
5. 傳入兩個及以上的數(shù)組時,它們元素數(shù)量將會相同。因為回調(diào)函數(shù)會并行地處理相互對應的元素。 如果幾個數(shù)組的元素數(shù)量不一致:空元素會擴展短那個數(shù)組,直到長度和最長的數(shù)組一樣。
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);
?>
//運行結(jié)果
Array
(
[0] => 1
[1] => 8
[2] => 27
[3] => 64
[4] => 125
)
Example #2:
?php
$func = function($value) {
return $value * 2;
};
print_r(array_map($func, range(1, 5)));
?>
//運行結(jié)果
Array
(
[0] => 2
[1] => 4
[2] => 6
[3] => 8
[4] => 10
)
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);
?>
//運行結(jié)果
// printout of $c
Array
(
[0] => The number 1 is called uno in Spanish
[1] => The number 2 is called dos in Spanish
[2] => The number 3 is called tres in Spanish
[3] => The number 4 is called cuatro in Spanish
[4] => The number 5 is called cinco in Spanish
)
// printout of $d
Array
(
[0] => Array
(
[1] => uno
)
[1] => Array
(
[2] => dos
)
[2] => Array
(
[3] => tres
)
[3] => Array
(
[4] => cuatro
)
[4] => Array
(
[5] => cinco
)
更多信息參考官方文檔:http://php.net/manual/zh/function.array-map.php
array_filter
* array_filter — 用回調(diào)函數(shù)過濾數(shù)組中的單元*
1. 依次將 array 數(shù)組中的每個值傳遞到 callback 函數(shù)。如果 callback 函數(shù)返回 true,則 array 數(shù)組的當前值會被包含在返回的結(jié)果數(shù)組中。數(shù)組的鍵名保留不變。
2. 如果沒有提供 callback 函數(shù), 將刪除 array 中所有等值為 FALSE 的條目。
3. ARRAY_FILTER_USE_KEY - callback接受鍵名作為的唯一參數(shù),ARRAY_FILTER_USE_BOTH - callback同時接受鍵名和鍵值
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"));
?>
//運行結(jié)果
Odd :
Array
(
[a] => 1
[c] => 3
[e] => 5
)
Even:
Array
(
[0] => 6
[2] => 8
[4] => 10
[6] => 12
Example #2:
?php
$entry = array(
0 => 'foo',
1 => false,
2 => -1,
3 => null,
4 => ''
);
print_r(array_filter($entry));
?>
//運行結(jié)果
Array
(
[0] => foo
[2] => -1
)
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));
?>
//運行結(jié)果
array(1) {
["b"]=>
int(2)
}
array(2) {
["b"]=>
int(2)
["d"]=>
int(4)
)
更多信息參考官方文檔:http://php.net/manual/zh/function.array-filter.php
參考其他鏈接: https://www.jb51.net/article/222535.htm
到此這篇關于php array_walk array_map array_filter區(qū)別案例詳解的文章就介紹到這了,更多相關php array_walk array_map array_filter區(qū)別內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
您可能感興趣的文章: 淺析php中array_map和array_walk的使用對比 php數(shù)組函數(shù)array_walk用法示例 php array_map與array_walk比較案例詳解