PHP: 如何从关联数组中删除行
PHP: How to delete rows from an associative array
我有一个关联数组,其中的键是datetime类型的数据(间隔15分钟)
array:37 [▼
"09:00" => Appointment {
#attributes: array:10 [▼
"id" => 1135
"startDateTime" => "2019-11-19 09:00:00"
"endDateTime" => "2019-11-19 09:45:00"
"duration" => 45
]
}
"09:15" => "" // I want to delete this row -> 15 minutes
"09:30" => "" // I want to delete this row -> 30 minutes end of the appointment
"09:45" => ""
"10:00" => Appointment {...duration => 60 ...}
"10:15" => "" // I want to delete this row -> 15 minutes
"10:30" => "" // I want to delete this row -> 30 minutes
"10:45" => "" // I want to delete this row -> 45 minutes
"11:00" => "" // I want to delete this row -> 60 minutes end of the appointment
"11:15" => ""
"11:30" => ""
"11:45" => "" Appointment {...duration => 15 ...}
...
]
此数组将提供 table,因此我想根据每次约会的持续时间删除后续行。我需要它,因为我想将约会跨越多行:
<td class="the-appointment" rowspan="{{ $appointment->duration / 15 }}">...
因此我需要从数组中删除后续行。
我这样做了:
$index = -1;
foreach ($row as $key => $appointment) {
if ($appointment) {
$loops = $appointment->duration / 15;
}
for ($i = 1; $i < $loops; $i++) {
unset($row[$index + 1]);
$index++;
}
}
array_push($calendar, $row);
但是因为是关联数组,所以无法得到循环的索引。有没有更聪明的方法来做到这一点?
开始的一些代码:
$duration = 0;
// I suggest to use array_filter and track `$duration` on each loop
$filtered = array_filter(
$apps,
function ($apm) use (&$duration) {
if ($duration === 0) {
if (!empty($apm->duration)) {
$duration = $apm->duration - 15;
return true;
} else {
return true;
}
} else {
$duration -= 15;
return false;
}
}
);
工作fiddlehere。
而不是使用 foreach()
,您可以使用 for()
并使用持续时间跳过您不需要的条目。结果放入$slots
.
基本上,遍历时隙中的时间,如果设置了持续时间,则将循环计数器增加约会的长度(duration/15 并使用 ceil()
向上舍入) ...
$row = ["09:00" => (object)["duration" => 40],
"09:15" => "",
"09:30" => "",
"09:45" => "",
"10:00" => (object)["duration" => 60],
"10:15" => "",
"10:30" => "",
"10:45" => "",
"11:00" => "",
"11:15" => "",
"11:30" => "",
"11:45" => (object)["duration" => 45]
];
$slots = [];
$times = array_keys($row);
for ( $i = 0; $i < count($times); $i++ ) {
$appointment = $row[$times[$i]];
$slots[$times[$i]] = $appointment;
if ( $appointment->duration ?? 0 > 0 ) {
$i += ceil($appointment->duration / 15);
}
}
print_r($slots);
根据测试数据,这给出...
(
[09:00] => stdClass Object
(
[duration] => 40
)
[10:00] => stdClass Object
(
[duration] => 60
)
[11:15] =>
[11:30] =>
[11:45] => stdClass Object
(
[duration] => 45
)
)
我有一个关联数组,其中的键是datetime类型的数据(间隔15分钟)
array:37 [▼
"09:00" => Appointment {
#attributes: array:10 [▼
"id" => 1135
"startDateTime" => "2019-11-19 09:00:00"
"endDateTime" => "2019-11-19 09:45:00"
"duration" => 45
]
}
"09:15" => "" // I want to delete this row -> 15 minutes
"09:30" => "" // I want to delete this row -> 30 minutes end of the appointment
"09:45" => ""
"10:00" => Appointment {...duration => 60 ...}
"10:15" => "" // I want to delete this row -> 15 minutes
"10:30" => "" // I want to delete this row -> 30 minutes
"10:45" => "" // I want to delete this row -> 45 minutes
"11:00" => "" // I want to delete this row -> 60 minutes end of the appointment
"11:15" => ""
"11:30" => ""
"11:45" => "" Appointment {...duration => 15 ...}
...
]
此数组将提供 table,因此我想根据每次约会的持续时间删除后续行。我需要它,因为我想将约会跨越多行:
<td class="the-appointment" rowspan="{{ $appointment->duration / 15 }}">...
因此我需要从数组中删除后续行。
我这样做了:
$index = -1;
foreach ($row as $key => $appointment) {
if ($appointment) {
$loops = $appointment->duration / 15;
}
for ($i = 1; $i < $loops; $i++) {
unset($row[$index + 1]);
$index++;
}
}
array_push($calendar, $row);
但是因为是关联数组,所以无法得到循环的索引。有没有更聪明的方法来做到这一点?
开始的一些代码:
$duration = 0;
// I suggest to use array_filter and track `$duration` on each loop
$filtered = array_filter(
$apps,
function ($apm) use (&$duration) {
if ($duration === 0) {
if (!empty($apm->duration)) {
$duration = $apm->duration - 15;
return true;
} else {
return true;
}
} else {
$duration -= 15;
return false;
}
}
);
工作fiddlehere。
而不是使用 foreach()
,您可以使用 for()
并使用持续时间跳过您不需要的条目。结果放入$slots
.
基本上,遍历时隙中的时间,如果设置了持续时间,则将循环计数器增加约会的长度(duration/15 并使用 ceil()
向上舍入) ...
$row = ["09:00" => (object)["duration" => 40],
"09:15" => "",
"09:30" => "",
"09:45" => "",
"10:00" => (object)["duration" => 60],
"10:15" => "",
"10:30" => "",
"10:45" => "",
"11:00" => "",
"11:15" => "",
"11:30" => "",
"11:45" => (object)["duration" => 45]
];
$slots = [];
$times = array_keys($row);
for ( $i = 0; $i < count($times); $i++ ) {
$appointment = $row[$times[$i]];
$slots[$times[$i]] = $appointment;
if ( $appointment->duration ?? 0 > 0 ) {
$i += ceil($appointment->duration / 15);
}
}
print_r($slots);
根据测试数据,这给出...
(
[09:00] => stdClass Object
(
[duration] => 40
)
[10:00] => stdClass Object
(
[duration] => 60
)
[11:15] =>
[11:30] =>
[11:45] => stdClass Object
(
[duration] => 45
)
)