Created
November 30, 2012 15:23
Array difference.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this also works : | |
$feed = array( | |
4000, | |
3500, | |
3000, | |
2000, | |
1000, | |
100, | |
0, | |
3000, | |
2000, | |
2500, | |
3000, | |
2000, | |
200, | |
50, | |
5000, | |
7000, | |
700, | |
100 | |
); | |
$compost = 0; | |
$countFeed = count($feed); | |
for($i = 1; $i < $countFeed; $i++){ | |
$prevNumber = $feed[$i - 1]; | |
$newNumber = $feed[$i]; | |
$diff = $prevNumber - $newNumber; | |
if($diff > 0) { | |
$compost = $diff + $compost; | |
} | |
} | |
echo $compost; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$feed = array( | |
4000, | |
3500, | |
3000, | |
2000, | |
1000, | |
100, | |
0, | |
3000 | |
); | |
want to sum the positive difference e.g. difference = -(new - old) | |
e.g. -(3500-4000) = 500 so won't sum when number goes up. | |
Any ideas? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$feed = array( | |
4000, | |
3500, | |
3000, | |
2000, | |
1000, | |
100, | |
0, | |
3000, | |
2000, | |
2500, | |
3000, | |
2000, | |
200, | |
50, | |
5000, | |
7000, | |
700, | |
100 | |
); | |
$compost = 0; | |
$countFeed = count($feed); | |
for($i = 0; $i < $countFeed; $i++){ | |
if($i > 0) { | |
$prevNumber = $feed[$i - 1]; | |
$newNumber = $feed[$i]; | |
$diff = $prevNumber - $newNumber; | |
if($diff > 0) { | |
$compost = $diff + $compost; | |
} | |
} | |
} | |
echo $compost; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment