Is it possible to change all values of an array without a loop in php?
This is a job for array_map()
(which will loop internally):
$a = array(0, 4, 5, 7);// PHP 5.3+ anonmymous function.$output = array_map(function($val) { return $val+1; }, $a);print_r($output);Array( [0] => 1 [1] => 5 [2] => 6 [3] => 8)
Edit by OP:
function doubleValues($a) { return array_map(function($val) { return $val * 2; }, $a);}
Yeah this is possible using the PHP function array_map() as mentioned in the other answers.This solutions are completely right und useful. But you should consider, that a simple foreach loop will be faster and less memory intense. Furthermore it grants a better readability for other programmers and users. Nearly everyone knows, what a foreach loop is doing and how it works, but most PHP users are not common with the array_map() function.