简体   繁体   English

动态地将日期分配给数组中的多个值 - php

[英]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.不知道怎么解释这个问题,但让我试试我有 2 个数组,第一个数组包括日期及其每日小时数,第二个数组是另一个单独的分小时数组,我需要创建新数组,其中包括带日期的分小时。

$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让我简单解释一下,我们有第一个日期 24 并且它的小时是 4 秒是 25 并且它的小时 4 现在查看分割数组它包含 2,3,3,2 所以第一个值和第二个值相加是 5 比第一个多24 小时,所以新数组看起来像这样 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:这是我如何可视化它以了解哪些日期 go 分为几个小时:

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

So i can see that divided hour:所以我可以看到分开的时间:

  • 2 goes with date 24 2 与日期 24
  • 3 with 24 and 25 3 与 24 和 25
  • 3 with 25 3 与 25
  • 2 with 26 (which is different than your expected result, so either you made a mistake or i didnt get the question right) 2 和 26(这与您的预期结果不同,所以要么您犯了错误,要么我没有正确回答问题)
  • 1 with 26 1 与 26

And I implemented it exactly like that.而我正是这样实现的。 First expanded the $dt array to have array of dates for every hour.首先将 $dt 数组扩展为每个小时的日期数组。 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.删除重复项,因为您不想在第一个小时内使用'date' => ['24', '24'] ,而只是单个值。

$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: $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"
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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