我有两个数组:
$DatesToDelete()包含一个日期列表,如下所示:
01/11/2022 02/11/2022 03/11/2022 04/11/2022 05/11/2022 06/11/2022 07/2022 07/2022 08/11/2022 09/11/ 20/11/2022 10/11/2022 11/2022 11/11/2022 12/11/2022 12/11/2022 13/11/2022 14/2022 15/11/2022 16/2022 17/2022 17/2022 18/11/2022 19/11/2022 20/20 20/11/2022 21/11/2022 /11/11/2022 23/11/2022 24/2022 24/202226/11/2022 27/11/2022 28/11/2022 29/11/2022 30/11/2022
$ArrayAllDates()包含另一个日期列表,如下所示:
07/11/2022 07/11/2022 18/11/2022 17/11/2022 02/12/2022
**我的目标是查找来自$DatesToDelete()的日期是否包含在$ArrayAllDates() **中
我不是PHP专家,我用foreach()尝试了很多循环,但都没有成功:
谢谢
foreach($ArrayAllDates as $d) {
foreach($DatesToDelete as $e) {
if(in_array($b,$arrayalldates)){
}
}
}发布于 2022-12-01 09:36:13
正如@DCodeMania所说,您可以使用array_intersect函数(https://www.php.net/manual/en/function.array-intersect.php)。
例如:
$datesToDelete = [
'02/11/2022',
'04/11/2022',
'06/11/2022',
'07/11/2022',
'02/12/2022',
];
$allDates = [
'07/11/2022',
'07/11/2022',
'18/11/2022',
'17/11/2022',
'02/12/2022',
];
$commonDates = array_intersect($allDates, $datesToDelete);
print_r($commonDates);
/* it print this:
Array
(
[0] => 07/11/2022
[1] => 07/11/2022
[4] => 02/12/2022
)
when the key is de key and value are of $allDates array
*/发布于 2022-12-01 08:48:36
你可以用这个循环
foreach($ArrayAllDates as $d){
if(in_array($d, $DatesToDelete)){
echo $d."<br>";
}
}如果要将此包含日期保存为数组
$containDates = array();
foreach($ArrayAllDates as $d){
if(in_array($d, $DatesToDelete)){
array_push($containDates ,$d);
}
}
print_r($containDates);https://stackoverflow.com/questions/74639012
复制相似问题