1

I have some array in PHP, the array seems like this:

array:2 [
"Habie" => array:2 [
    "2019-06-30" => array:2 [
      "duration" => "4 Hour"
      "status" => "Less"
    ]
    "2019-06-26" => array:2 [
      "duration" => "11 Hour"
      "status" => "More"
    ]
    "total" => "15 Hour"
  ]
"Budiman" => array:3 [
    "2019-06-28" => array:2 [
      "duration" => "4 Hour"
      "status" => "Less"
    ]
    "2019-06-25" => array:2 [
      "duration" => "3 Hour"
      "status" => "Less"
    ]
    "2019-06-21" => array:2 [
      "duration" => "9 Hour"
      "status" => "Enough"
    ]
    "total" => "16 Hour"
  ]

]

And I want to compare with this data (Which is the Interval Date):

array:30 [
  0 => "2019-06-30"
  1 => "2019-06-29"
  2 => "2019-06-28"
  3 => "2019-06-27"
  4 => "2019-06-26"
  5 => "2019-06-25"
  6 => "2019-06-24"
  7 => "2019-06-23"
  8 => "2019-06-22"
  9 => "2019-06-21"
  10 => "2019-06-20"
  11 => "2019-06-19"
  12 => "2019-06-18"
  13 => "2019-06-17"
  14 => "2019-06-16"
  15 => "2019-06-15"
  16 => "2019-06-14"
  17 => "2019-06-13"
  18 => "2019-06-12"
  19 => "2019-06-11"
  20 => "2019-06-10"
  21 => "2019-06-09"
  22 => "2019-06-08"
  23 => "2019-06-07"
  24 => "2019-06-06"
  25 => "2019-06-05"
  26 => "2019-06-04"
  27 => "2019-06-03"
  28 => "2019-06-02"
  29 => "2019-06-01"
]

And then the key with name: Habie has nested date keys as complete like this even though on that date, habie didn't have data:

(THE EXPECTED OUTPUT DATA LIKE THIS BELOW CODE)

array:2 [
"Habie" => array:2 [
    "2019-06-30" => array:2 [
      "duration" => "4 Hour"
      "status" => "Less"
    ]
    "2019-06-29" => []
    "2019-06-27" => []
    "2019-06-26" => array:2 [
      "duration" => "11 Hour"
      "status" => "More"
    ]
    "2019-06-25" => []
    "2019-06-24" => []
    "2019-06-23" => []
    "2019-06-22" => []
    "2019-06-21" => []
    "2019-06-20" => []
    "2019-06-19" => []
    "2019-06-18" => []
    "2019-06-17" => []
    "2019-06-16" => []
    "2019-06-15" => []
    "2019-06-14" => []
    "2019-06-13" => []
    "2019-06-12" => []
    "2019-06-11" => []
    "2019-06-10" => []
    "2019-06-09" => []
    "2019-06-08" => []
    "2019-06-07" => []
    "2019-06-06" => []
    "2019-06-05" => []
    "2019-06-04" => []
    "2019-06-03" => []
    "2019-06-02" => []
    "2019-06-01" => []
    "total" => "15 Hour"
  ]

And Budiman same like that too.

I've tried many step, one of them like this:

//$dataAttGroupByDate is the raw array                
foreach ($dataAttGroupByDate as $knama => $tanggalan) {
    $arr=array_keys($tanggalan);
    array_pop($arr);
    $i=0;

    foreach ($tanggalInterval as $kdrs => $datastaff) {  
        if(\is_numeric(array_search($tanggalInterval[$kdrs],$arr))) {
            $i++;
            continue;
        } elseif(array_search($tanggalInterval[$i],$arr)==false) {
            $dataAttGroupByDate[$knama] = $tanggalInterval[$i];
            $i++;
        }
    }
}

but I have no solution for this. I hope someone can resolve this problem and help me out. :)

thank you :D

dWinder
  • 11,597
  • 3
  • 24
  • 39
Habie Smart
  • 139
  • 1
  • 9

1 Answers1

1

You can use array_flip and array_merge to achieve that by:

First fill array with the dates as keys and empty arrays:

$template = array_map(function ($e) {return [];}, array_flip($dates));

Then merge for each sub array:

foreach($arr as $k => &$v)
    $v = array_merge($template, $v);

Live example: 3v4l

dWinder
  • 11,597
  • 3
  • 24
  • 39
  • I find it fascinating that your plain English instruction said "**fill array** with the dates as **keys**" yet you did not implement the most logical, native function -- `array_fill_keys()`. – mickmackusa Sep 28 '22 at 11:25