根据满足的条件检索数组
Retrieve arrays based on the conditions met
我有一组数组。我想检索不可用的最近日期。
例如,我提供了一个日期01-05-2022
。我得到的结果应该是 room_id
4,因为它是最近的日期。
如果我提供日期 05-05-2022
,结果将是 room_id
6.
$array = [
0 => [
"room_id" => "1",
"date" => "01-05-2022"
"available" => "A"
],
1 => [
"room_id" => "2",
"date" => "02-05-2022"
"available" => "A"
],
2 => [
"room_id" => "3",
"date" => "03-05-2022"
"available" => "A"
],
3 => [
"room_id" => "4",
"date" => "04-05-2022"
"available" => "U"
],
4 => [
"room_id" => "5",
"date" => "05-05-2022"
"available" => "A"
],
5 => [
"room_id" => "6",
"date" => "06-05-2022"
"available" => "U"
],
]
我是这样做的,但我没有得到任何结果:
$nextUnavailableDate = [];
foreach ($array as $item) {
$selectedDate = date('Y-m-d', strtotime("01-05-2022"));
$itemDate = date('Y-m-d', strtotime($item['date']));
if ($selectedDate < $itemDate) {
$q = array_filter($item, function($value) {
return $value['availability'] === 'U';
});
if ($q) {
$nextUnavailableDate = $q;
}
}
}
感谢任何帮助。任何解释也很感激,因为我还在学习 PHP。谢谢!
你能试试这个吗?
$nextUnavailableDate = [];
foreach ($array as $item) {
$selectedDate = date('Y-m-d', strtotime("01-05-2022"));
$itemDate = date('Y-m-d', strtotime($item['date']));
if ($selectedDate < $itemDate) {
// retrieving all the `availability` keys that has the value of U
// will give you a list of availability value of U results
if ($item['availability'] === 'U') {
$nextUnavailableDate[] = $item;
}
}
}
// will then try to retrieve the first array since it's the nearest value that you requested
var_dump(array_shift(array_values($nextUnavailableDate)));
我有一组数组。我想检索不可用的最近日期。
例如,我提供了一个日期01-05-2022
。我得到的结果应该是 room_id
4,因为它是最近的日期。
如果我提供日期 05-05-2022
,结果将是 room_id
6.
$array = [
0 => [
"room_id" => "1",
"date" => "01-05-2022"
"available" => "A"
],
1 => [
"room_id" => "2",
"date" => "02-05-2022"
"available" => "A"
],
2 => [
"room_id" => "3",
"date" => "03-05-2022"
"available" => "A"
],
3 => [
"room_id" => "4",
"date" => "04-05-2022"
"available" => "U"
],
4 => [
"room_id" => "5",
"date" => "05-05-2022"
"available" => "A"
],
5 => [
"room_id" => "6",
"date" => "06-05-2022"
"available" => "U"
],
]
我是这样做的,但我没有得到任何结果:
$nextUnavailableDate = [];
foreach ($array as $item) {
$selectedDate = date('Y-m-d', strtotime("01-05-2022"));
$itemDate = date('Y-m-d', strtotime($item['date']));
if ($selectedDate < $itemDate) {
$q = array_filter($item, function($value) {
return $value['availability'] === 'U';
});
if ($q) {
$nextUnavailableDate = $q;
}
}
}
感谢任何帮助。任何解释也很感激,因为我还在学习 PHP。谢谢!
你能试试这个吗?
$nextUnavailableDate = [];
foreach ($array as $item) {
$selectedDate = date('Y-m-d', strtotime("01-05-2022"));
$itemDate = date('Y-m-d', strtotime($item['date']));
if ($selectedDate < $itemDate) {
// retrieving all the `availability` keys that has the value of U
// will give you a list of availability value of U results
if ($item['availability'] === 'U') {
$nextUnavailableDate[] = $item;
}
}
}
// will then try to retrieve the first array since it's the nearest value that you requested
var_dump(array_shift(array_values($nextUnavailableDate)));