按键拆分关联数组

时间:2017-01-12 10:25:00

标签: php

我有这样的数组:

private $schedule = [
    "13:00" => true,
    "13:30" => false,
    "14:00" => true,
    "14:30" => true,
    "15:00" => true,
    "15:30" => true,
    "16:00" => true,
]

例如当前时间是“14:00”。

我想将此数组拆分为两个数组:

在“14:00”之前

 private $schedule1 = [
    "13:00" => true,
    "13:30" => false,
    "14:00" => true
]

在“14:00”之后

private $schedule2 = [
   "14:30" => true,
   "15:00" => true,
   "15:30" => true,
   "16:00" => true,
]

有没有办法按键(“14:00”)这样做?

6 个答案:

答案 0 :(得分:2)

您可以使用foreach。结果: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);

答案 1 :(得分:2)

带有标志的简单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.

答案 2 :(得分:1)

使用foreach循环检查密钥的简单函数可以解决它。您可以尝试以下方式:

  $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');

或者您只需拨打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);

答案 3 :(得分:1)

来自@Mark Ba​​ker的最佳答案

$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);

答案 4 :(得分:1)

<?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
?>

答案 5 :(得分:0)

您可以尝试其他方式: -

$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;
    }
}

此代码甚至适用于随机密钥。试一试。