Does PHP have an equivalent to Python's list comprehension syntax? Does PHP have an equivalent to Python's list comprehension syntax? arrays arrays

Does PHP have an equivalent to Python's list comprehension syntax?


Maybe something like this?

$out=array_map(function($x) {return $x*$x;}, range(0, 9))

This will work in PHP 5.3+, in an older version you'd have to define the callback for array_map separately

function sq($x) {return $x*$x;}$out=array_map('sq', range(0, 9));


PHP 5.5 may support list comprehensions - see the mailing list announcement:

And further discussion: