我在我的例子中有2个法院预订阵列,用于5个法院的特定日期和时间,包括日期,法院编号,成员和类型。 我想从第二个数组中删除第一个数组中匹配日期/球场的项目。
这是一个时段。第一个数组显示此时段中的2个预订。
Array
(
[0] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 1
[2] => Annie
[3] => Regular
)
[1] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 3
[2] => Meredith
[3] => Regular
)
)
第二个数组是该日期/时间可用的所有时间段。
Array
(
[2] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 1
[2] =>
[3] => UNBOOKED
)
[3] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 2
[2] =>
[3] => UNBOOKED
)
[4] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 3
[2] =>
[3] => UNBOOKED
)
[5] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 4
[2] =>
[3] => UNBOOKED
)
[6] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 5
[2] =>
[3] => UNBOOKED
)
)
我想要做的是删除第二个数组中与元素[0]和[1]匹配的项目。我尝试过的每个解决方案都只搜索数组中的字符串,而不是另一个数组。 理想情况下,结果看起来像这样(外部数字键对我来说并不重要)。
Array
(
[0] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 1
[2] => Annie
[3] => Regular
)
[1] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 3
[2] => Meredith
[3] => Regular
)
[3] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 2
[2] =>
[3] => UNBOOKED
)
[5] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 4
[2] =>
[3] => UNBOOKED
)
[6] => Array
(
[0] => Jul 6, 2014 7:00 AM
[1] => 5
[2] =>
[3] => UNBOOKED
)
)
答案 0 :(得分:0)
这是您使用数组过滤器删除预订时间列表中存在的可用时间列表中的条目的方法。
您可以在此处查看此操作:http://codepad.viper-7.com/VVbnys
$regular = Array(
0 => Array (
0 => 'Jul 6, 2014 7:00 AM',
1 => 1,
2 => 'Annie',
3 => 'Regular',
),
1 => Array (
0 => 'Jul 6, 2014 7:00 AM',
1 => 3,
2 => 'Meredith',
3 => 'Regular',
)
);
$unbooked = Array (
2 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 1,
2 => '',
3 => 'UNBOOKED',
),
3 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 2,
2 => '',
3 => 'UNBOOKED',
),
4 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 3,
2 => '',
3 => 'UNBOOKED',
),
5 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 4,
2 => '',
3 => 'UNBOOKED',
),
6 => Array
(
0 => 'Jul 6, 2014 7:00 AM',
1 => 5,
2 => '',
3 => 'UNBOOKED',
),
);
//filter the unbooked array
$unbooked = array_filter($unbooked, function($availSlot) use ($regular){
//loop over each taken slot
foreach($regular as $takenSlot){
//if keys 0 and 1 are the same in both taken and available...
if($availSlot[0] == $takenSlot[0] && $availSlot[1] == $takenSlot[1]){
//return false to remove this element from the array
return false;
}
}
//if we get here, no matches were found.
//return true so the element stays in the array
return true;
});
理想情况下,格式将更改为多维数组,允许您在时间段和可用数字下嵌套内容,而无需遍历每个条目以查找匹配的条目。