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

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

问题描述

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

他们做同样的事情吗?
它们可以互换使用吗?

如果它们完全不同,我将感谢您提供说明性示例的帮助.

解决方案

  • 更改值:
  • 数组键访问:
  • 返回值:
    • array_map 返回一个新数组,array_walk 只返回 true.因此,如果你不想因为遍历一个数组而创建一个数组,你应该使用 array_walk.
  • 迭代多个数组:
    • array_map 也可以接收任意数量的数组,并且可以遍历它们并行,而 array_walk 只对一个操作.
  • 向回调传递任意数据:
    • array_walk 可以接收一个额外的任意参数传递给回调.自 PHP 5.3(当引入 匿名函数 时)以来,这几乎无关紧要.
  • 返回数组的长度:
    • array_map 的结果数组与最大输入数组的长度相同;array_walk 不返回数组,但同时不能改变原数组的元素个数;array_filter 根据过滤函数只选择数组元素的一个子集.它确实保留了密钥.

示例:

<?php$origarray1 = array(2.4, 2.6, 3.5);$origarray2 = array(2.4, 2.6, 3.5);print_r(array_map('floor', $origarray1));//$origarray1 保持不变//改变 $origarray2array_walk($origarray2, function (&$v, $k) { $v = floor($v); });打印_r($origarray2);//这是array_walk 的一种更恰当的使用array_walk($origarray1, function ($v, $k) { echo "$k => $v", "\n"; });//array_map 接受多个数组打印_r(array_map(function ($a, $b) { return $a * $b; }, $origarray1, $origarray2));//只选择 > 的元素2.5打印_r(array_filter($origarray1, function ($a) { return $a > 2.5; }));?>

结果:

数组([0] =>2[1] =>2[2] =>3)大批([0] =>2[1] =>2[2] =>3)0 =>2.41 =>2.62 =>3.5大批([0] =>4.8[1] =>5.2[2] =>10.5)大批([1] =>2.6[2] =>3.5)

What exactly is 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.

解决方案

  • Changing Values:
  • Array Keys Access:
  • Return Value:
    • 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.
  • Iterating Multiple Arrays:
    • array_map also can receive an arbitrary number of arrays and it can iterate over them in parallel, while array_walk operates only on one.
  • Passing Arbitrary Data to Callback:
    • 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).
  • Length of Returned Array:
    • 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.

Example:

<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>

Result:

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天全站免登陆