简体   繁体   中英

Assign date to multiple values in array dynamically - php

Don't know hot to explain you this issue but let me try i have 2 array first array includes date and its daily hours and second array is another separate array of divided hours i need to create new array which include divided hours with dates.

$dt = array(
    "24" => array("date" => "24", "hour" => 4),
    "25" => array("date" => "25", "hour" => 4), 
    "26" => array("date" => "26", "hour" => 3),
);

$dvided_hours = array(2, 3, 3, 2, 1);

let me explain in brief, we have first date 24 and its hour is 4 second is 25 and its hour 4 now looked at divided array it contains 2,3,3,2 so first value and second value addition is 5 is more than first 24's hour so new array will looked like this 2 = 24, 3 = 24 & 25

new array looked like this

array(
    0 => array(
        "hour" => "2",
        "date" => array(0 => "24")
    ),
    1 => array(
        "hour" => "3",
        "date" => array(0 => "24", 1 => "25")
    ),
    2 => array(
        "hour" => "3",
        "date" => array(0 => "25")
    ),
    3 => array(
        "hour" => "2",
        "date" => array(0 => "25")
    ),
    4 => array(
        "hour" => "1",
        "date" => array(0 => "26")
    ),
)

Please help me I'm stuck in this since 2 days. Thanks in advance

Here is how i visualized this to know which dates go into which divided hours:

   23321
24 |
24 |
24  |
24  |
25  |
25   |
25   |
25   |
26    |
26    |
26     |

So i can see that divided hour:

  • 2 goes with date 24
  • 3 with 24 and 25
  • 3 with 25
  • 2 with 26 (which is different than your expected result, so either you made a mistake or i didnt get the question right)
  • 1 with 26

And I implemented it exactly like that. First expanded the $dt array to have array of dates for every hour. Then went through divided hours and for each single hour shifted one date from beginning of that array. Removed duplicates because you dont want to have 'date' => ['24', '24'] for first divided hour, but just single value.

$dt = array(
    "24" => array("date" => "24", "hour" => 4),
    "25" => array("date" => "25", "hour" => 4), 
    "26" => array("date" => "26", "hour" => 3),
);

$divided_hours = array(2, 3, 3, 2, 1);

$expandedHours = [];
foreach ($dt as $d) {
  foreach (range(0, $d['hour']-1) as $h) {
    $expandedHours[] = $d['date'];
  }
}
var_dump($expandedHours);

count($expandedHours) === array_sum($divided_hours) or die('wrong hours setup');

foreach ($divided_hours as $dh) {
  $result = ['hour' => $dh, 'date' => []];
  foreach (range(0, $dh-1) as $h) {
    $result['date'][] = array_shift($expandedHours);
  }

  $result['date'] = array_unique($result['date'], SORT_NUMERIC);
  $results[] = $result;
}
var_dump($results);

$expandedHours:

array(11) {
  [0]=>
  string(2) "24"
  [1]=>
  string(2) "24"
  [2]=>
  string(2) "24"
  [3]=>
  string(2) "24"
  [4]=>
  string(2) "25"
  [5]=>
  string(2) "25"
  [6]=>
  string(2) "25"
  [7]=>
  string(2) "25"
  [8]=>
  string(2) "26"
  [9]=>
  string(2) "26"
  [10]=>
  string(2) "26"
}

$results:

array(5) {
  [0]=>
  array(2) {
    ["hour"]=>
    int(2)
    ["date"]=>
    array(1) {
      [0]=>
      string(2) "24"
    }
  }
  [1]=>
  array(2) {
    ["hour"]=>
    int(3)
    ["date"]=>
    array(2) {
      [0]=>
      string(2) "24"
      [2]=>
      string(2) "25"
    }
  }
  [2]=>
  array(2) {
    ["hour"]=>
    int(3)
    ["date"]=>
    array(1) {
      [0]=>
      string(2) "25"
    }
  }
  [3]=>
  array(2) {
    ["hour"]=>
    int(2)
    ["date"]=>
    array(1) {
      [0]=>
      string(2) "26"
    }
  }
  [4]=>
  array(2) {
    ["hour"]=>
    int(1)
    ["date"]=>
    array(1) {
      [0]=>
      string(2) "26"
    }
  }
}

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