php - PHP | Python map() 等价物
问题描述
PHP 有与 Python 的 Map()-Function 等价的功能吗?
如果没有,是否可以自己构建它?
提前致谢!
解决方案
扩展 vivek_23 的评论。
Python
items = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, items))
print(squared) // [1, 4, 9, 16, 25]
PHP (< 7.4)
$items = [1, 2, 3, 4, 5];
$squared = array_map(function($x) { return $x ** 2; }, $items);
var_dump($squared); // [1, 4, 9, 16, 25]
PHP (7.4 +)
自 7.4 版以来,箭头函数已被引入 PHP。
$items = [1, 2, 3, 4, 5];
$squared = array_map(fn($x) => $x ** 2, $items);
var_dump($squared); // [1, 4, 9, 16, 25]
推荐阅读
- javascript - Wavify javascript 代码在 wordpress 中不起作用(未捕获的 TypeError:无法读取属性 'getBoundingClientRect' of null)
- c# - 将列表框中的数据编辑到我的 SQL 数据库
- excel - 在多个显示器中打开 Excel 文件
- owl - rdflib "EnumeratedClass" 创建枚举数据类型
- bash - 检查 bash 脚本的输出
- apache-kafka-streams - nullpointer exception in aggregate operation in kafka stream
- windows - 有人一起使用过统一写入过滤器 (uwfmgr) 和 netsh 吗?运行 netsh 然后重新启动后,我看到了一些问题
- java - Can I ignore a getter-based write-only property during deserialization in Java Jackson?
- javascript - Reducing a 2D array taking into account another 2D array
- java - how to override default hazlecast.xml configuration?