Newbie here in PHP programming. I have some question regarding arrays. How can I get the difference between adjacent values in an array?
$value = array(2, 5, 9,
@Gumbo you added $diffs = array(); it has to be $value
<?php
$value = array(2,5,9,10,19);
for ($i = 1, $n = count($value); $i < $n; $i++) {
$diffs[] = $value[$i] - $value[$i-1];
}
echo "<pre>";
print_r($diffs);
echo "</pre>";
?>
Just use a simple for
loop:
$diffs = array();
for ($i = 1, $n = count($value); $i < $n; $i++) {
$diffs[] = $value[$i] - $value[$i-1];
}