Home  >  Article  >  Backend Development  >  array_map/array_filter/array_walk traverses the array in the same way as foreach. Who wants to be faster?

array_map/array_filter/array_walk traverses the array in the same way as foreach. Who wants to be faster?

WBOY
WBOYOriginal
2016-10-22 00:14:251392browse

array_walk is equivalent to foreach:

$arr = ['Client'=>'jQuery','Server'=>'PHP'];
array_walk($arr, function($v, $k) {
    echo "键:$k 值:$v\n";
});

For example, remove the leading and trailing whitespace of the array $arr element:

array_walk($arr, function(&$v) { $v = trim($v); });
foreach($arr as &$v) { $v = trim($v); }
array_filter: 用回调函数过滤数组中的单元,返回过滤后的数组

var_export(
    array_filter([1, 2, 3], function($v) {
        return $v > 1;
    })
);
和
foreach([1, 2, 3] as $k => $v) {
    if($v > 1) {
        $tmp[$k] = $v;
    }
}
var_export($tmp);
都输出:
array (
  1 => 2,
  2 => 3,
)

PHP array mapping reduction (MapReduce):

array_map/array_reduce
array_map: 将回调函数作用到给定数组的单元上
var_export(
    array_map(function ($v) {
        return $v * $v;
    }, [1, 2, 3])
);
和
foreach([1, 2, 3] as $v) {
    $tmp[] = $v * $v;
}
var_export($tmp);
都输出:
array (
  0 => 1,
  1 => 4,
  2 => 9,
)

array_reduce: Use the callback function to iteratively reduce the array into a single value
//Output 16, which is 10+1+2+3, with 10 as the initial value.

echo array_reduce([1, 2, 3], function($result, $item) {
    $result = $result + $item;
    return $result;
}, 10);
用foreach表达:
$result = 10;
foreach([1, 2, 3] as $v) {
    $result = $result + $v;
}
echo $result;

The execution effects are the same, but is it faster to use functions or foreach?

Reply content:

array_walk is equivalent to foreach:

$arr = ['Client'=>'jQuery','Server'=>'PHP'];
array_walk($arr, function($v, $k) {
    echo "键:$k 值:$v\n";
});
For example, remove the leading and trailing whitespace of the array $arr element:

array_walk($arr, function(&$v) { $v = trim($v); });
foreach($arr as &$v) { $v = trim($v); }
array_filter: 用回调函数过滤数组中的单元,返回过滤后的数组

var_export(
    array_filter([1, 2, 3], function($v) {
        return $v > 1;
    })
);
和
foreach([1, 2, 3] as $k => $v) {
    if($v > 1) {
        $tmp[$k] = $v;
    }
}
var_export($tmp);
都输出:
array (
  1 => 2,
  2 => 3,
)
PHP array mapping reduction (MapReduce):

array_map/array_reduce
array_map: 将回调函数作用到给定数组的单元上
var_export(
    array_map(function ($v) {
        return $v * $v;
    }, [1, 2, 3])
);
和
foreach([1, 2, 3] as $v) {
    $tmp[] = $v * $v;
}
var_export($tmp);
都输出:
array (
  0 => 1,
  1 => 4,
  2 => 9,
)
array_reduce: Use the callback function to iteratively reduce the array into a single value

//Output 16, which is 10+1+2+3, with 10 as the initial value.

echo array_reduce([1, 2, 3], function($result, $item) {
    $result = $result + $item;
    return $result;
}, 10);
用foreach表达:
$result = 10;
foreach([1, 2, 3] as $v) {
    $result = $result + $v;
}
echo $result;
The execution effects are the same, but is it faster to use functions or foreach?
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn