PHP | Python map() equivalent



php是否等于pythons map() - 函数?

如果不是,是否可以自行构建?

预先感谢!

在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版。

以来
$items = [1, 2, 3, 4, 5];
$squared = array_map(fn($x) => $x ** 2, $items);
var_dump($squared); // [1, 4, 9, 16, 25]

最新更新