[英]Split associative array by key
I have array like this: 我有这样的数组:
private $schedule = [
"13:00" => true,
"13:30" => false,
"14:00" => true,
"14:30" => true,
"15:00" => true,
"15:30" => true,
"16:00" => true,
]
For example current time is "14:00". 例如,当前时间是“ 14:00”。
I want is to split this array into two arrays: 我想将这个数组分成两个数组:
before "14:00" 在“ 14:00”之前
private $schedule1 = [
"13:00" => true,
"13:30" => false,
"14:00" => true
]
after "14:00" 在“ 14:00”之后
private $schedule2 = [
"14:30" => true,
"15:00" => true,
"15:30" => true,
"16:00" => true,
]
Is there way to do this by key ("14:00")? 有什么办法可以通过按键(“ 14:00”)进行操作吗?
You can use foreach
. 您可以使用foreach
。 Result: https://eval.in/714864 结果: https : //eval.in/714864
<?php
$schedule = [
"13:00" => true,
"13:30" => false,
"14:00" => true,
"14:30" => true,
"15:00" => true,
"15:30" => true,
"16:00" => true,
];
$schedule1 = [];
$schedule2 = [];
$current_time = "14:00"; //date("H:i");
echo 'Current Time: '. $current_time;
echo '<br />';
foreach($schedule as $key => $val){
if($key<=$current_time){
$schedule1[$key] = $val;
}else{
$schedule2[$key] = $val;
}
}
print_r($schedule);
echo '<br />';
print_r($schedule1);
echo '<br />';
print_r($schedule2);
Simple foreach()
iteration with a flag could attain this:- 具有标志的简单foreach()
迭代可以达到以下目的:
$curr = '14:00';
$schedule1 = $schedule2 = array();
//maintain a flag which would track when the required time key is reached
$timeReached = FALSE;
foreach ($schedule as $key => $val) {
if (!$timeReached) {
$schedule1[$key] = $val;
}
else {
$schedule2[$key] = $val;
}
//set the flag when the key is reached.
if ($key == $curr) {
$timeReached = TRUE;
}
}
print_r($schedule1);
print_r($schedule2); // would be the required array.
$before1400 = array_filter($schedule, function($key) {
return $key <= '14:00'; },
ARRAY_FILTER_USE_KEY);
$after1400 = array_filter($schedule, function($key) {
return $key > '14:00'; },
ARRAY_FILTER_USE_KEY);
<?php
$split = array_search('14:00', $schedule);
print_r(array_slice($data, 0, $split)) // first part
print_r(array_slice($data, $split + 1)); // second part
?>
A simple function using a foreach
loop to check the keys could solve it. 使用foreach
循环检查键的简单函数可以解决该问题。 You can try something like: 您可以尝试如下操作:
$schedule = [
"13:00" => true,
"13:30" => false,
"14:00" => true,
"14:30" => true,
"15:00" => true,
"15:30" => true,
"16:00" => true,
]
function array_split($array, $delimiter) {
$first_half = [];
$second_half = [];
foreach (array_keys($array) as $key) {
if ($key < $delimiter) {
$first_half[$key] = $array[$key];
}
else {
$second_half[$key] = $array[$key];
}
}
return [$first_half, $second_half];
}
list($first_half, $second_half) = array_split($schedule, '14:00');
or you could do away with just a couple of calls to array_filter
: 或者您可以只对array_filter
进行几个调用:
$first_half = array_filter($schedule, function($key) { return $key <= '14:00'; }, ARRAY_FILTER_USE_KEY);
$second_half = array_filter($schedule, function($key) { return $key '14:00'; }, ARRAY_FILTER_USE_KEY);
You can try other way as:- 您可以尝试其他方式:
$schedule1 = [];
$schedule2 = [];
$flag = false;
foreach ($schedule as $k => $v) {
if ($k == date("H:i")) {
$schedule1[] = $v;
$flag = true;
}
if ($flag) {
$schedule2[] = $v;
} else {
$schedule1[] = $v;
}
}
This code will work for even random keys. 该代码甚至适用于随机密钥。 Give it a try. 试试看。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.