Skip to content
Advertisement

Convert string to multidimensional array to calculate date in php

I have a string as below

Mon[3,9] Tue[3,9] Wed[5,9] Thu[5,11] Fri[5,11] Sat[5,11] Sun[4,10]

I want to break this string to multidimensional array and store days like Mon,Tue,Wed as keys and the values inside square brackets as values for each day as below and access each day as smaller arrays

Array
(
    [Mon] => [3,9]
    [Tue] => [3,9]
    [Wed] => [5,9]
    [Thu] => [5,11]
    [Fri] => [5,11]
    [Sat] => [5,11]
    [Sun] => [4,10]
)

with this below code I was able to achieve it but the value such as [3,9] or [5,11] are being treated as strings

    $atd = $utd = "";
    $dod = "20-12-2020";
    $daysArray = "Mon[3,9] Tue[3,9] Wed[5,9] Thu[5,11] Fri[5,11] Sat[5,11] Sun[4,10]";
    $days = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
    preg_match_all("/[[^]]*]/", $daysArray, $matches);
    $dayNum = $matches[0];
    $daysArray = array_combine($days , $dayNum);
    print_r($daysArray);
        
        $valArray = array();
        
        foreach($daysArray as $day=>$val){
            if($day == "Mon" || $day == "Tue"){
              $atd = date("d-m-Y", strtotime("+".$val[0]." days", $dod));
              $utd = date("d-m-Y", strtotime("+".$val[1]." days", $dod));
           }else if($day == "Sun"){
              $atd = date("d-m-Y", strtotime("+".$val[0]." days", $dod));
              $utd = date("d-m-Y", strtotime("+".$val[1]." days", $dod));
           }else if($day == "Wed"){
              $atd = date("d-m-Y", strtotime("+".$val[0]." days", $dod));
              $utd = date("d-m-Y", strtotime("+".$val[1]." days", $dod));
           }else{
              $atd = date("d-m-Y", strtotime("+".$val[0]." 5 days", $dod));
              $utd = date("d-m-Y", strtotime("+".$val[1]." days", $dod));
             }
            }
        } 

when I print $valArray it is a array like below which is very bad.

array(
   [0]=>[
   [1]=>4
   [2]=>,
   [3]=>1
   [4]=>0
   [5]=>]
);

Please help

Advertisement

Answer

I think this is the result you were asking for.

$str = 'Mon[3,9] Tue[3,9] Wed[5,9] Thu[5,11] Fri[5,11] Sat[5,11] Sun[4,10]';

$result = [];

$bits = explode(' ', $str); // explode on space between day names
foreach ( $bits as $bit) {
    $b = explode('[', $bit);    // $bit = Mon[3,9] 
    $b[1] = rtrim($b[1], ']');  // $b0 = Mon, B1 = 3,9] so trim off the last ]
    $result[$b[0]] = explode(',', $b[1]); // explode on , to get inner array
}
print_r($result);

Result

Array
(
    [Mon] => Array
        (
            [0] => 3
            [1] => 9
        )
    [Tue] => Array
        (
            [0] => 3
            [1] => 9
        )
    [Wed] => Array
        (
            [0] => 5
            [1] => 9
        )
    [Thu] => Array
        (
            [0] => 5
            [1] => 11
        )
    [Fri] => Array
        (
            [0] => 5
            [1] => 11
        )
    [Sat] => Array
        (
            [0] => 5
            [1] => 11
        )
    [Sun] => Array
        (
            [0] => 4
            [1] => 10
        )
)
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement