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

WBOY
Release: 2016-10-22 00:14:25
Original
1477 people have browsed it

array_walk is equivalent to foreach:

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

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

<code>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,
)</code>
Copy after login
Copy after login

PHP array mapping reduction (MapReduce):

<code>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,
)</code>
Copy after login
Copy after login

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.

<code>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;</code>
Copy after login
Copy after login

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

Reply content:

array_walk is equivalent to foreach:

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

<code>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,
)</code>
Copy after login
Copy after login
PHP array mapping reduction (MapReduce):

<code>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,
)</code>
Copy after login
Copy after login
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.

<code>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;</code>
Copy after login
Copy after login
The execution effects are the same, but is it faster to use functions or foreach?
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!