array_map,array_walk和array_filter之间的区别 [英] Difference between array_map, array_walk and array_filter

查看:182
本文介绍了array_map,array_walk和array_filter之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

array_maparray_walkarray_filter之间的区别到底是什么?我从文档中可以看到,您可以传递一个回调函数来对提供的数组执行操作.但是我似乎没有发现它们之间有什么特别的区别.

What exactly is the the difference between array_map, array_walk and array_filter. What I could see from documentation is that you could pass a callback function to perform an action on the supplied array. But I don't seem to find any particular difference between them.

他们执行相同的事情吗?
它们可以互换使用吗?

Do they perform the same thing?
Can they be used interchangeably?

如果这些示例有所不同,请提供示例帮助,谢谢您.

I would appreciate your help with illustrative example if they are different at all.

推荐答案

  • 更改值:
    • array_map 不能更改输入数组中的值,而 array_map 永远不要更改其参数.
      • Changing Values:
        • array_map cannot change the values inside input array(s) while array_walk can; in particular, array_map never changes its arguments.
          • array_map returns a new array, array_walk only returns true. Hence, if you don't want to create an array as a result of traversing one array, you should use array_walk.
          • array_map 也可以接收任意数量的数组,并且可以并行遍历它们,而<一个href ="http://php.net/array_walk" rel ="noreferrer"> array_walk 只能在一个上运行.
          • array_walk 可以接收一个额外的任意参数以传递给回调.自PHP 5.3(引入匿名函数以来),这基本上无关紧要. /li>
          • array_walk can receive an extra arbitrary parameter to pass to the callback. This mostly irrelevant since PHP 5.3 (when anonymous functions were introduced).
          • 所得的array_map数组的长度与最大输入数组的长度相同; array_walk不返回数组,但同时不能更改原始数组的元素数; array_filter 根据过滤功能仅选择数组元素的子集.它确实保留了密钥.
          • The resulting array of array_map has the same length as that of the largest input array; array_walk does not return an array but at the same time it cannot alter the number of elements of original array; array_filter picks only a subset of the elements of the array according to a filtering function. It does preserve the keys.

          示例:

          <pre>
          <?php
          
          $origarray1 = array(2.4, 2.6, 3.5);
          $origarray2 = array(2.4, 2.6, 3.5);
          
          print_r(array_map('floor', $origarray1)); // $origarray1 stays the same
          
          // changes $origarray2
          array_walk($origarray2, function (&$v, $k) { $v = floor($v); }); 
          print_r($origarray2);
          
          // this is a more proper use of array_walk
          array_walk($origarray1, function ($v, $k) { echo "$k => $v", "\n"; });
          
          // array_map accepts several arrays
          print_r(
              array_map(function ($a, $b) { return $a * $b; }, $origarray1, $origarray2)
          );
          
          // select only elements that are > 2.5
          print_r(
              array_filter($origarray1, function ($a) { return $a > 2.5; })
          );
          
          ?>
          </pre>
          

          结果:

          Array
          (
              [0] => 2
              [1] => 2
              [2] => 3
          )
          Array
          (
              [0] => 2
              [1] => 2
              [2] => 3
          )
          0 => 2.4
          1 => 2.6
          2 => 3.5
          Array
          (
              [0] => 4.8
              [1] => 5.2
              [2] => 10.5
          )
          Array
          (
              [1] => 2.6
              [2] => 3.5
          )
          

          这篇关于array_map,array_walk和array_filter之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆