Array_map/array_filter/array_walk the way the array is traversed is like foreach, who wants to be faster?

Source: Internet
Author: User
Array_walk equivalent to foreach:

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

For example, remove the front and back blanks of the array $arr elements:

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 Map Simplification (MapReduce):

array_map/array_reducearray_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: Iterating over the array simplification (reduce) to a single value using a callback function
Output 16, which is 10+1+2+3, where 10 is 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 effect is the same, but the specific use of the function or foreach faster AH

Reply content:

Array_walk equivalent to foreach:

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

For example, remove the front and back blanks of the array $arr elements:

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 Map Simplification (MapReduce):

array_map/array_reducearray_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: Iterating over the array simplification (reduce) to a single value using a callback function
Output 16, which is 10+1+2+3, where 10 is 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 effect is the same, but the specific use of the function or foreach faster AH

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.