Say I have an array that looks like this:
array(100) {
[0]=>
array(4) {
["player_id"]=>
string(2) "jk"
["date"]=>
string(10) "2012-11-07"
["hits"]=>
string(4) "1000"
}
[1]=>
array(4) {
["player_id"]=>
string(2) "jk"
["date"]=>
string(10) "2012-11-14"
["hits"]=>
string(4) "2000"
}
[2]=>
array(4) {
["player_id"]=>
string(2) "mc"
["date"]=>
string(10) "2012-11-14"
["hits"]=>
string(4) "1500"
}
[3]=>
array(4) {
["player_id"]=>
string(2) "mc"
["date"]=>
string(10) "2012-11-07"
["hits"]=>
string(4) "2300"
}
…
And this continues. So, basically I have several players and for each player I have two dates. I would like to end up having an array like this:
array(100) {
["jk"]=>
array(2) {
["hits_today"]=>
string(4) "1000"
["hits_difference"]=>
string(5) "-1000"
}
["mc"]=>
array(2) {
["hits_today"]=>
string(4) "1500"
["hits_difference"]=>
string(4) "-800"
}
..
Basically I want to process the first array knowing that for each player I (might) have two values, and create an array of arrays that as keys uses the player_id and as components with the difference between those two values, and the value of today.
My concerns are:
- What is the best way to process the first array?
- How would I manage if the value of today doesn’t exist, or the value of tomorrow doesn’t? How can I control that while processing the array?
If this comes from a query I would do this instead: