在我的getNearestMeeting
方法中,它似乎允许数组具有相同的key
和相同的value
。
我想检查一下是否已key
和meeting_id
值。正如您在下面看到的那样,我的代码到目前为止的结果返回了两个键== 6
,并且它们具有相同的meeting_id
== 1812
。
我做错了什么?
$pupils = array("parent" => array( array ("pupil_name" => "Daniel", "pupil_id" => "5", "pupil_grade" => "87"),
array ("pupil_name" => "Daniel", "pupil_id" => "5", "pupil_grade" => "86"),
array ("pupil_name" => "Callum", "pupil_id" => "6", "pupil_grade" => "87")
));
$meetings = array( array("slot_id" => "1", "meeting_id" => "1812", "startTime" => "2016-10-07 14:30:00",
"endTime" => "2016-10-07 14:35:00", "grade_id" => "87"),
array("slot_id" => "2", "meeting_id" => "1812", "startTime" => "2016-10-07 14:35:00",
"endTime" => "2016-10-07 14:40:00", "grade_id" => "87"),
array("slot_id" => "3", "meeting_id" => "1812", "startTime" => "2016-10-07 14:40:00",
"endTime" => "2016-10-07 14:45:00", "grade_id" => "87"),
array("slot_id" => "4", "meeting_id" => "1813", "startTime" => "2016-10-07 15:05:00",
"endTime" => "2016-10-07 15:10:00", "grade_id" => "-1"),
array("slot_id" => "6", "meeting_id" => "1813", "startTime" => "2016-10-07 15:10:00",
"endTime" => "2016-10-07 15:15:00", "grade_id" => "-1"),
array("slot_id" => "9", "meeting_id" => "1813", "startTime" => "2016-10-07 15:20:00",
"endTime" => "2016-10-07 15:25:00", "grade_id" => "-1"),
array("slot_id" => "10", "meeting_id" => "1813", "startTime" => "2016-10-07 15:30:00",
"endTime" => "2016-10-07 15:35:00", "grade_id" => "88"),
array("slot_id" => "11", "meeting_id" => "1813", "startTime" => "2016-10-07 15:40:00",
"endTime" => "2016-10-07 15:45:00", "grade_id" => "88"),
array("slot_id" => "17", "meeting_id" => "1813", "startTime" => "2016-10-07 15:50:00",
"endTime" => "2016-10-07 15:50:00", "grade_id" => "88")
);
function in_array_r($array, $key, $val) {
foreach ($array as $item) {
if (key($item) == $key && isset($item[$key]) && $item[$key]["meeting_id"] == $val)
return true;
else
return false;
}
}
function getNearestMeeting($meetings){
global $pupils;
$ignore = array();
$current_pupil_id = "5";
$current_pupil_grade_id = "87";
foreach($meetings as $item){
if($current_pupil_grade_id === $item["grade_id"]){
$earliest = strtotime($item["endTime"]);
$ignore[$current_pupil_id][] = array("meeting_id" => $item["meeting_id"], "endTime" => $item["endTime"]);
break;
}
}
$counter = 0;
foreach($pupils["parent"] as $item){
$counter++;
foreach($meetings as $item2){
$lastArray = end(end($ignore));
$previousTime = strtotime($lastArray["endTime"]);
if(!in_array_r($ignore, $item["pupil_id"], $item2["meeting_id"])) {
if(isset($previousTime) && strtotime($item2["startTime"]) >= $previousTime && strtotime($item2["startTime"]) >= $earliest){
if($item["pupil_grade"] === $item2["grade_id"]){
$ignore[$item["pupil_id"]][] = array("meeting_id" => $item2["meeting_id"], "endTime" => $item2["endTime"]);
}
}
}
}
}
print_r($ignore);
}
getNearestMeeting($meetings);
来自print_r($ignore);
的结果:
Array
(
[0] => Array
(
[5] => Array
(
[meeting_id] => 1812
[endTime] => 2016-10-07 14:35:00
)
)
[1] => Array
(
[6] => Array
(
[meeting_id] => 1812
[endTime] => 2016-10-07 14:40:00
)
)
[2] => Array
(
[6] => Array
(
[meeting_id] => 1812
[endTime] => 2016-10-07 14:45:00
)
)
)
答案 0 :(得分:1)
After the discussion in the Stack Overflow PHP Chat,发现的问题如下:
$ignore
数组的结构奇怪,如果一个学生有多个会议,它会创建多个顶级数组,而它应该只使用一个引用该特定学生的数组。in_array_r
功能结构不正确。最终修复导致以下更改:
$ignore
数组的添加更改为:
$ignore[$item["pupil_id"]][] = array("meeting_id" => $item2["meeting_id"], "endTime" => $item2["endTime"]);
进行额外检查以确保$ignore[$item["public_id"]]
是一个数组,如果不是,则在向其追加新项目之前创建它。
更改in_array_r
函数以反映新的数组结构:
function in_array_r($array, $key, $val) {
if(array_key_exists($key, $array)){
foreach ($array[$key] as $item) {
if ($item["meeting_id"] === $val)
return true;
else
return false;
}
}
}