考虑以下数组。他们代表 和 的前 5 名表现最佳的yesterday
员工today
:
$yesterday = array(
6 => array('name' => 'Tod', 'score' => 9.5),
12 => array('name' => 'Jim', 'score' => 7.3),
18 => array('name' => 'Bob', 'score' => 8.4),
7 => array('name' => 'Jan', 'score' => 6.2),
20 => array('name' => 'Sam', 'score' => 6.0),
);
$today = array(
6 => array('name' => 'Tod', 'score' => 9.1),
9 => array('name' => 'Jef', 'score' => 9.3),
35 => array('name' => 'Axl', 'score' => 7.6),
7 => array('name' => 'Jan', 'score' => 6.5),
41 => array('name' => 'Ted', 'score' => 8.0),
);
我需要从上面编译的 3 个新数组:$stay
持有昨天进入前 5 名的员工,今天仍然是,$gone
,持有昨天进入前 5 名但不再是的员工,以及$new
,持有新人到$today
榜首-5 名单:
// notice that the scores in $stay come from $today, not $yesterday
// also notice that index keys are maintained
$stay = array(
6 => array('name' => 'Tod', 'score' => 9.1),
7 => array('name' => 'Jan', 'score' => 6.5)
);
$gone = array(
12 => array('name' => 'Jim', 'score' => 7.3),
18 => array('name' => 'Bob', 'score' => 8.4),
20 => array('name' => 'Sam', 'score' => 6.0)
);
$new = array(
9 => array('name' => 'Jef', 'score' => 9.3),
35 => array('name' => 'Axl', 'score' => 7.6),
41 => array('name' => 'Ted', 'score' => 8.0)
);
我不知道如何在这里建立逻辑。我从一个循环开始,但没有走远。我相信它应该是这样的。你能帮我解决这个问题吗?
for ($i = 0; $i < count($yesterday); $i++) {
// I'm comparing key numbers, but not key values
// how do I compare key values?
if (in_array($yesterday[$i], $today) {
// add to $stay array
}
else {
// add to $gone array
}
}
for ($i = 0; $i < count($today); $i++) {
if (!in_array($today[$i], $yesterday) {
// add to $new array
}
}
PS我不知道这是否有帮助,但总是$yesterday
等$today
长(在这种情况下,5个项目,但其他情况下两个数组都可以容纳7或10个项目)。$stay
和的组合项在$new
逻辑上总是等于$yesterday
或$today
:-)中的项数