Skip to content
Advertisement

Split 1 day array into 2 days array

I have an array like this and I want to split this into 2 arrays with the same date.

 array (size=2)
   'dates' => string '2021-10-20' (length=10)
   'hours' => 
     array (size=2)
       0 => 
         array (size=12)
           0 => string '07:30' (length=5)
           1 => string '08:00' (length=5)
           2 => string '08:30' (length=5)
           3 => string '09:00' (length=5)
           4 => string '09:30' (length=5)
           5 => string '10:00' (length=5)
           6 => string '10:30' (length=5)
           7 => string '11:00' (length=5)
           8 => string '11:30' (length=5)
           9 => string '12:00' (length=5)
           10 => string '12:30' (length=5)
           11 => string '13:00' (length=5)
       1 => 
         array (size=4)
           0 => string '15:30' (length=5)
           1 => string '16:00' (length=5)
           2 => string '16:30' (length=5)
           3 => string '17:00' (length=5)

I want to make 2 arrays with the same date, and it looks like this:

 0 =>
   'dates' => string '2021-10-20'
   'hours' => 
     array (size=1)
       0 => 
         array (size=12)
           0 => string '07:30' (length=5)
           1 => string '08:00' (length=5)
           2 => string '08:30' (length=5)
           3 => string '09:00' (length=5)
           4 => string '09:30' (length=5)
           5 => string '10:00' (length=5)
           6 => string '10:30' (length=5)
           7 => string '11:00' (length=5)
           8 => string '11:30' (length=5)
           9 => string '12:00' (length=5)
           10 => string '12:30' (length=5)
           11 => string '13:00' (length=5)
 1 =>
   'dates' => string '2021-10-20'
   'hours' => 
     array (size=1)
       0 => 
         array (size=4)
           0 => string '15:30' (length=5)
           1 => string '16:00' (length=5)
           2 => string '16:30' (length=5)
           3 => string '17:00' (length=5)

How to make this happen? and my code looks like this,

if ($cekWaktuDosen4->num_rows == 0) {
    $timesDosen4[$i]['dates'] = $dates;
    $timesDosen4[$i]['hours'] = Array
    (
        (0) => Array
            (
                0 => '07:30',
                1 => '08:00',
                2 => '08:30',
                3 => '09:00',
                4 => '09:30',
                5 => '10:00',
                6 => '10:30',
                7 => '11:00',
                8 => '11:30',
                9 => '12:00',
                10 => '12:30',
                11 => '13:00',
                12 => '13:30',
                13 => '14:00',
                14 => '14:30',
                15 => '15:00',
                16 => '15:30',
                17 => '16:00',
                18 => '16:30',
                19 => '17:00'
            )
    );
}else {
    $y = 0;
    while ($k = $cekWaktuDosen4->fetch_object()) {
        $tglMulaiDosen4 = $carbon->create($carbon->parse($k->tanggal_mulai)->format('Y-m-d'));
        $tglSelesaiDosen4 = $carbon->create($carbon->parse($k->tanggal_selesai)->format('Y-m-d'));
        $tglBantuMulai4 = $tglMulaiDosen4->addHours(7)->addMinutes(30);
        $tglBantuSelesai4 = $tglSelesaiDosen4->addHours(17);
        if($tglBantuMulai4 < $k->tanggal_mulai) {
            $timesDosen4[$i]['dates'] = $tglMulaiDosen4->format('Y-m-d');
            $timesDosen4[$i]['hours'][$y] = create_time_range($tglBantuMulai4->format('H:i'), rounddown_timestamp($carbon->parse($k->tanggal_mulai)->format('H:i')));
            $y++;
            if($k->tanggal_selesai < $tglBantuSelesai4) {
                $timesDosen4[$i]['hours'][$y] = create_time_range(roundup_timestamp($carbon->parse($k->tanggal_selesai)->format('H:i')), $tglBantuSelesai4->format('H:i'));
            }
        }else if($tglBantuMulai4 == $k->tanggal_mulai && $tglBantuSelesai4 != $k->tanggal_selesai) {
            $timesDosen4[$i]['dates'] = $tglMulaiDosen4->format('Y-m-d');
            $timesDosen4[$i]['hours'][$y] = create_time_range(roundup_timestamp($carbon->parse($k->tanggal_selesai)->format('H:i')), $tglBantuSelesai4->format('H:i'));
        }
    }
}

i tried to add variable for dates but its not works. And sometimes just showing last arrays. Help me to split an array into 2 different arrays

Advertisement

Answer

For single dimensional array:

$newArray = array();

foreach($originalArray['hours'] as $k=>$hours){
    $newArray[$k]['dates'] = $originalArray['dates'];
    $newArray[$k]['hours'] = $hours;
}

Output: https://3v4l.org/oE3hh

For Multidimensional array:

$newArray = array();

foreach($originalArray as $originalArr){
    foreach($originalArr['hours'] as $k=>$hours){
        $newArray[$originalArr['dates']][$k]['dates'] = $originalArr['dates'];
        $newArray[$originalArr['dates']][$k]['hours'] = $hours;
    }
}
$newArray = array_values($newArray);

print_r($newArray);

Output: https://3v4l.org/PAppV

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement