Python 具有语法甜美的列表推导:
S = [x**2 for x in range(10)]
print S;
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
在 PHP 中,我需要做一些循环:
$output = array();
$Nums = range(0,9);
foreach ($Nums as $num)
{
$out[] = $num*=$num;
}
print_r($out);
要得到:
数组 ( [0] => 0 [1] => 1 [2] => 4 [3] => 9 [4] => 16 [5] => 25 [6] => 36 [7] => 49 [8] => 64 [9] => 81)
无论如何在PHP中获得类似的列表理解语法吗?无论如何,PHP 5.3 中的任何新功能都可以做到这一点吗?
谢谢!