I would like to update the values of a multidimensionnal php array : $a
array(1) {
["test"]=> array(4) {
[0]=> int(5)
[1]=> int(7)
[2]=> int(10)
[3]=> int(14)
}
}
For $a["test"][$i], I would like to obtain this new value $a["test"][$i] - $a["test"][$i-1].
In this case the resulting array would be :
array(1) {
["test"]=> array(4) {
[0]=> int(5)
[1]=> int(2)
[2]=> int(3)
[3]=> int(4)
}
}
Of course, I could do it with a foreach loop and a new array
$new = array();
foreach($a as $k=>$v){
for($i=0;$i<=3;$i++){
$new[$k][$i] = $v[$i] - $v[$i-1];
}
}
$a = $new;
var_dump($a);
but is this the best way? Just out of curiosity, I was wondering if using array_walk to make it could be nicer and, generally, how/if array_walk could access the previous/next value in the array.
Thank you a lot !