>>> lst = ['dingo', 'wombat', 'wallaby']
>>> [w.title() for w in lst]
['Dingo', 'Wombat', 'Wallaby']
>>>
In python there is simple ways to todo with list comprehension.
What about in php with array('dingo', 'wombat', 'wallaby'); ?
Are there array comprehension or any build in function ,or normally loop on it?
EDIT
function addCaps( Iterator $it )
{
echo ucfirst( $it->current() ) . '<br />';
return true;
}
/*** an array of aussies ***/
$array = array( 'dingo', 'wombat', 'wallaby' );
try
{
$it = new ArrayIterator( $array );
iterator_apply( $it, 'addCaps', array($it) );
}
catch(Exception $e)
{
/*** echo the error message ***/
echo $e->getMessage();
}
Look the code not too simple like I expected?
You can use
array_map()with anonymous functions (closures are PHP 5.3+ only).Output
Edit: OP’s sample code
Output: