PHP 如何根据日期排列数组键值
PHP how to arrange array key values based on date
我想根据数组值日期(从 - 到)排列值的数组键,例如下面是我的数组,我想从这个数组中安排预订日期。
Array
(
[0] => Array
(
[id] => 56
[user_id] => 12
[name] => A
[email] => hhjhj@df.com
[phone] => 4522111111
[service_name] => a:1:{i:0;s:14:"Beard modeling";}
[booking_date] => 2021-01-01
[time] => 18:30:00
[barbar_id] => 11
[created_date] => 2021-01-01 12:11:35
)
[1] => Array
(
[id] => 57
[user_id] => 12
[name] => B
[email] => hhjhj@df.com
[phone] => 4522111111
[service_name] => a:1:{i:0;s:14:"Beard modeling";}
[booking_date] => 2021-01-09
[time] => 18:30:00
[barbar_id] => 11
[created_date] => 2021-01-01 12:11:35
)
[2] => Array
(
[id] => 58
[user_id] => 12
[name] => C
[email] => hhjhj@df.com
[phone] => 4522111111
[service_name] => a:1:{i:0;s:14:"Beard modeling";}
[booking_date] => 2021-01-14
[time] => 18:30:00
[barbar_id] => 11
[created_date] => 2021-01-01 12:11:35
)
[3] => Array
(
[id] => 61
[user_id] => 12
[name] => GGG
[email] => d@fdfdf.com
[phone] => 4522111111
[service_name] => a:1:{i:0;s:14:"Beard modeling";}
[booking_date] => 2021-01-17
[time] => 18:30:00
[barbar_id] => 11
[created_date] => 2021-01-01 12:11:35
)
[4] => Array
(
[id] => 59
[user_id] => 12
[name] => k
[email] => hhjhj@df.com
[phone] => 4522111111
[service_name] => a:1:{i:0;s:14:"Beard modeling";}
[booking_date] => 2021-01-28
[time] => 18:30:00
[barbar_id] => 11
[created_date] => 2021-01-01 12:11:35
)
)
我想安排booking_date喜欢
booking_date : 2021-01-01 To 2021-01-09
booking_date : 2021-01-14 To 2021-01-17
booking_date : 2021-01-17 To 2021-01-28
我试过下面的代码,但没有考虑 booking_date : 2021-01-28
foreach ($getUsers as $kt=> $value) {
if($kt % 2 ==0){
echo "booking_date".$getUsers[$kt]['booking_date'].'</br>';
$from = $getUsers[$kt]['booking_date'];
}else{
echo "booking_date ELSE".$getUsers[$kt]['booking_date'].'</br>';
// echo "EEE".$sql.'</br>';
$to =$from.' To ' .$getUsers[$kt]['booking_date'];
}
}
有人请帮助我,我必须在我的脚本中更改什么?
尝试这样的事情:
for($i=0;$i<=count($getUsers);$i+2) {
if(isset($getUsers[$i]))
echo "booking_date: ".$getUsers[$i]['booking_date'].'-'.$getUsers[$i+1]['booking_date'].'</br>';
}
}
使用for bucle,增量为2。创建区间为"index" - "index+1"。
例如,您可以使用此代码:
foreach ($array as $kt => $value) {
if ($kt % 2 !== 0) {
continue;
}
$to = null;
if(isset($array[$kt + 1])) {
$to = ' To ' .$array[$kt + 1]['booking_date'];
}
echo 'booking_date: ' . $value['booking_date'] . $to . '<br>';
}
对于您想要输出的原始问题:
booking_date : 2021-01-01 To 2021-01-09
booking_date : 2021-01-14 To 2021-01-17
booking_date : 2021-01-17 To 2021-01-28
我认为 array_chunk
会很方便,因为您要将项目配对在一起以创建 'from' 和 'to'。
解决方案的另一部分只是跟踪倒数第二个 booking_date
,因此我们可以将其用作最后一个元素的 from
。
我也添加了对只有 1 个元素的数组的处理,因为我不太确定你的用例。
<?php
$userBookings = [
0 => [ 'booking_date' => '2021-01-01' ],
1 => [ 'booking_date' => '2021-01-09' ],
2 => [ 'booking_date' => '2021-01-14' ],
3 => [ 'booking_date' => '2021-01-17' ],
4 => [ 'booking_date' => '2021-01-28' ]
];
$userBookingsPairs = array_chunk($userBookings, 2);
$fromDate = $toDate = $previousToDate = '';
foreach ($userBookingsPairs as $userBookingsChunk) {
$isPair = count($userBookingsChunk) === 2;
if ($isPair) {
$from = reset($userBookingsChunk);
$to = end($userBookingsChunk);
$fromDate = $from['booking_date'];
$previousToDate = $toDate = $to['booking_date'];
} elseif (!empty($previousToDate)) {
// Last element following pairs
$to = reset($userBookingsChunk);
$fromDate = $previousToDate;
$toDate = $to['booking_date'];
} else {
// Single element only
$to = reset($userBookingsChunk);
$toDate = $to['booking_date'];
echo "booking_date: {$toDate}<br>";
break;
}
echo "booking_date: {$fromDate} To {$toDate}<br>";
}
关于你的第二个问题:
2021-01-01 To 2021-01-09 , 2021-01-09 To 2021-01-14 , 2021-01-14 To 2021-01-17 And 2021-01-17 To 2021-01-28
所以在基本术语中,对于前 2 个元素,我们从元素 #1 和元素 #2 中取出 booking_date
并将它们组合起来。
对于接下来的 2 个元素,我们将元素 #2 的 booking_date
与元素 #3 组合,然后将元素 #3 与元素 #4 组合。依此类推,直到最后一个元素,我们将元素 #n-1 与元素 #n.
组合在一起
<?php
$userBookings = [
0 => [ 'booking_date' => '2021-01-01' ],
1 => [ 'booking_date' => '2021-01-09' ],
2 => [ 'booking_date' => '2021-01-14' ],
3 => [ 'booking_date' => '2021-01-17' ],
4 => [ 'booking_date' => '2021-01-28' ]
];
$output = '';
$userBookingsPairs = array_chunk($userBookings, 2);
$fromDate = $toDate = $previousFromDate = $previousToDate = '';
foreach ($userBookingsPairs as $userBookingsChunk) {
$isPair = count($userBookingsChunk) === 2;
if ($isPair) {
$from = reset($userBookingsChunk);
$to = end($userBookingsChunk);
$fromDate = $from['booking_date'];
$toDate = $to['booking_date'];
if (empty($previousFromDate)) {
// First iteration
$output = "$fromDate To $toDate";
} else { // 'n'th iteration
// print date range using prior element
$output .= ", $previousToDate To $fromDate";
// print date range for this element
$output .= ", $fromDate To $toDate";
}
$previousFromDate = $fromDate;
$previousToDate = $toDate;
} elseif (!empty($previousToDate)) {
// Last element following pairs
$to = reset($userBookingsChunk);
$output .= ", $previousToDate To {$to['booking_date']}";
} else {
// Single element only
$to = reset($userBookingsChunk);
$output = $to['booking_date'];
}
}
echo $output;
我想根据数组值日期(从 - 到)排列值的数组键,例如下面是我的数组,我想从这个数组中安排预订日期。
Array
(
[0] => Array
(
[id] => 56
[user_id] => 12
[name] => A
[email] => hhjhj@df.com
[phone] => 4522111111
[service_name] => a:1:{i:0;s:14:"Beard modeling";}
[booking_date] => 2021-01-01
[time] => 18:30:00
[barbar_id] => 11
[created_date] => 2021-01-01 12:11:35
)
[1] => Array
(
[id] => 57
[user_id] => 12
[name] => B
[email] => hhjhj@df.com
[phone] => 4522111111
[service_name] => a:1:{i:0;s:14:"Beard modeling";}
[booking_date] => 2021-01-09
[time] => 18:30:00
[barbar_id] => 11
[created_date] => 2021-01-01 12:11:35
)
[2] => Array
(
[id] => 58
[user_id] => 12
[name] => C
[email] => hhjhj@df.com
[phone] => 4522111111
[service_name] => a:1:{i:0;s:14:"Beard modeling";}
[booking_date] => 2021-01-14
[time] => 18:30:00
[barbar_id] => 11
[created_date] => 2021-01-01 12:11:35
)
[3] => Array
(
[id] => 61
[user_id] => 12
[name] => GGG
[email] => d@fdfdf.com
[phone] => 4522111111
[service_name] => a:1:{i:0;s:14:"Beard modeling";}
[booking_date] => 2021-01-17
[time] => 18:30:00
[barbar_id] => 11
[created_date] => 2021-01-01 12:11:35
)
[4] => Array
(
[id] => 59
[user_id] => 12
[name] => k
[email] => hhjhj@df.com
[phone] => 4522111111
[service_name] => a:1:{i:0;s:14:"Beard modeling";}
[booking_date] => 2021-01-28
[time] => 18:30:00
[barbar_id] => 11
[created_date] => 2021-01-01 12:11:35
)
)
我想安排booking_date喜欢
booking_date : 2021-01-01 To 2021-01-09
booking_date : 2021-01-14 To 2021-01-17
booking_date : 2021-01-17 To 2021-01-28
我试过下面的代码,但没有考虑 booking_date : 2021-01-28
foreach ($getUsers as $kt=> $value) {
if($kt % 2 ==0){
echo "booking_date".$getUsers[$kt]['booking_date'].'</br>';
$from = $getUsers[$kt]['booking_date'];
}else{
echo "booking_date ELSE".$getUsers[$kt]['booking_date'].'</br>';
// echo "EEE".$sql.'</br>';
$to =$from.' To ' .$getUsers[$kt]['booking_date'];
}
}
有人请帮助我,我必须在我的脚本中更改什么?
尝试这样的事情:
for($i=0;$i<=count($getUsers);$i+2) {
if(isset($getUsers[$i]))
echo "booking_date: ".$getUsers[$i]['booking_date'].'-'.$getUsers[$i+1]['booking_date'].'</br>';
}
}
使用for bucle,增量为2。创建区间为"index" - "index+1"。
例如,您可以使用此代码:
foreach ($array as $kt => $value) {
if ($kt % 2 !== 0) {
continue;
}
$to = null;
if(isset($array[$kt + 1])) {
$to = ' To ' .$array[$kt + 1]['booking_date'];
}
echo 'booking_date: ' . $value['booking_date'] . $to . '<br>';
}
对于您想要输出的原始问题:
booking_date : 2021-01-01 To 2021-01-09
booking_date : 2021-01-14 To 2021-01-17
booking_date : 2021-01-17 To 2021-01-28
我认为 array_chunk
会很方便,因为您要将项目配对在一起以创建 'from' 和 'to'。
解决方案的另一部分只是跟踪倒数第二个 booking_date
,因此我们可以将其用作最后一个元素的 from
。
我也添加了对只有 1 个元素的数组的处理,因为我不太确定你的用例。
<?php
$userBookings = [
0 => [ 'booking_date' => '2021-01-01' ],
1 => [ 'booking_date' => '2021-01-09' ],
2 => [ 'booking_date' => '2021-01-14' ],
3 => [ 'booking_date' => '2021-01-17' ],
4 => [ 'booking_date' => '2021-01-28' ]
];
$userBookingsPairs = array_chunk($userBookings, 2);
$fromDate = $toDate = $previousToDate = '';
foreach ($userBookingsPairs as $userBookingsChunk) {
$isPair = count($userBookingsChunk) === 2;
if ($isPair) {
$from = reset($userBookingsChunk);
$to = end($userBookingsChunk);
$fromDate = $from['booking_date'];
$previousToDate = $toDate = $to['booking_date'];
} elseif (!empty($previousToDate)) {
// Last element following pairs
$to = reset($userBookingsChunk);
$fromDate = $previousToDate;
$toDate = $to['booking_date'];
} else {
// Single element only
$to = reset($userBookingsChunk);
$toDate = $to['booking_date'];
echo "booking_date: {$toDate}<br>";
break;
}
echo "booking_date: {$fromDate} To {$toDate}<br>";
}
关于你的第二个问题:
2021-01-01 To 2021-01-09 , 2021-01-09 To 2021-01-14 , 2021-01-14 To 2021-01-17 And 2021-01-17 To 2021-01-28
所以在基本术语中,对于前 2 个元素,我们从元素 #1 和元素 #2 中取出 booking_date
并将它们组合起来。
对于接下来的 2 个元素,我们将元素 #2 的 booking_date
与元素 #3 组合,然后将元素 #3 与元素 #4 组合。依此类推,直到最后一个元素,我们将元素 #n-1 与元素 #n.
<?php
$userBookings = [
0 => [ 'booking_date' => '2021-01-01' ],
1 => [ 'booking_date' => '2021-01-09' ],
2 => [ 'booking_date' => '2021-01-14' ],
3 => [ 'booking_date' => '2021-01-17' ],
4 => [ 'booking_date' => '2021-01-28' ]
];
$output = '';
$userBookingsPairs = array_chunk($userBookings, 2);
$fromDate = $toDate = $previousFromDate = $previousToDate = '';
foreach ($userBookingsPairs as $userBookingsChunk) {
$isPair = count($userBookingsChunk) === 2;
if ($isPair) {
$from = reset($userBookingsChunk);
$to = end($userBookingsChunk);
$fromDate = $from['booking_date'];
$toDate = $to['booking_date'];
if (empty($previousFromDate)) {
// First iteration
$output = "$fromDate To $toDate";
} else { // 'n'th iteration
// print date range using prior element
$output .= ", $previousToDate To $fromDate";
// print date range for this element
$output .= ", $fromDate To $toDate";
}
$previousFromDate = $fromDate;
$previousToDate = $toDate;
} elseif (!empty($previousToDate)) {
// Last element following pairs
$to = reset($userBookingsChunk);
$output .= ", $previousToDate To {$to['booking_date']}";
} else {
// Single element only
$to = reset($userBookingsChunk);
$output = $to['booking_date'];
}
}
echo $output;