<?php
$bigArray = array(
array('John','2012-03-29',1),
array('Doe','2012-03-30',1),
array('John','2012-03-31',2),
array('Doe','2012-03-31',5),
array('Tom','2012-03-31',5),
array('John','2012-04-02',5),
array('John','2012-04-02',21),
array('John','2012-03-07',21)
);
$cache = array();
foreach ($bigArray as $v) {
if (isset($cache[$v[1]])) {
if ($cache[$v[1][1]] == $v[2]) {
echo "Equal";
$cache[$v[1]] = array($v[0].','.$cache[$v[1]][0], $v[2]);//append user to same value
}
else if ($cache[$v[1][1]] < $v[2]) {
echo "Replacing value! ".$cache[$v[1]][0]. " to " .$v[0]."<br/>";
$cache[$v[1]] = array($v[0], $v[2]);
}
} else {
$cache[$v[1]] = array($v[0], $v[2]);
}
}
print_r($cache);
?>
This script finds the highest value for a particular date and saves it to a new array $cache
However when checking if the highest value is equal on the same date, it returns false?
array(
array('Doe','2012-03-31',5),
array('Tom','2012-03-31',5),
)
The above in the array is what is confusing me. Shouldn’t it be counted as a match?
The output:
Replacing value! John to Doe
Replacing value! Doe to Tom
Replacing value! John to John
Array
(
[2012-03-29] => Array
(
[0] => John
[1] => 1
)
[2012-03-30] => Array
(
[0] => Doe
[1] => 1
)
[2012-03-31] => Array
(
[0] => Tom
[1] => 5
)
[2012-04-02] => Array
(
[0] => John
[1] => 21
)
[2012-03-07] => Array
(
[0] => John
[1] => 21
)
)
Without going into too much detail,
$v[1][1]seems to be quite nonsensical to me. It refers to the second character of the date string in your original array. You probably mean: