简体   繁体   中英

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".

I want is to split this array into two arrays:

before "14:00"

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

after "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")?

You can use foreach . Result: 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:-

$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. 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 :

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM