In PHP, I have an array:
$a = array(10, 15, 20, 25, 30);
I want to add 5% to each element of the array $a
, so that it looks like this:
$a = array(10.5, 15.75, 21, 26.25, 31.5);
I do not want to use foreach
.
Advertisement
Answer
Try this
<?php $a = array(10, 15, 20, 25, 30);; $output = array_map(function($val) { return $val + ($val*5/100); }, $a); print_r($output); ?>
Output :-
Array ( [0] => 10.5 [1] => 15.75 [2] => 21 [3] => 26.25 [4] => 31.5 )