<?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);
?>
此脚本查找特定日期的最大值并将其保存到新数组$cache
但是,当检查同一日期的最高值是否相等时,它返回 false?
array(
array('Doe','2012-03-31',5),
array('Tom','2012-03-31',5),
)
数组中的上述内容让我感到困惑。不应该算比赛吗?
输出:
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
)
)