3

给定以下格式的 2 个数组:

$array1 = array("1", "2", "3", "4", "5");

$array2 = array(
  0 => array("start" => "09:00", "end" => "17:00"),
  1 => array("start" => "18:00", "end" => "20:00")
);

我需要合并,结果是:

$result = array(
  array(
    "start_day" => "1",
    "start_time" => "09:00",
    "end_day" => "1",
    "end_time" => "17:00"
  ),
  array(
    "start_day" => "1",
    "start_time" => "18:00",
    "end_day" => "1",
    "end_time" => "20:00"
 )
 // and so on for each item in $array1
);

两个数组可以有不同的长度,但 $array2 中的每个项目都必须应用于 $array1 中的一个项目。只是把它扔出去看看是否有人对这种合并有任何经验。我当前的解决方案只给了我一个长度等于 $array2 长度的结果数组。现在正在处理这个问题,但任何见解都将不胜感激!

4

1 回答 1

1

像这样:

$result = [];
foreach($array1 as $elem1) {
  foreach($array2 as $elem2) {
    $result[] = array(
      "start_day" => $elem1,
      "start_time" => $elem2['start'],
      "end_day" => $elem1,
      "end_time" => $elem2['end']
    );
  }
}
于 2013-09-03T19:38:57.120 回答