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的任何新功能都可以做到吗?
谢谢!