duande1985 2013-09-03 19:21
浏览 229
已采纳

合并不同长度的数组

Given 2 arrays of the following formats:

$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")
);

I need to merge so that the result is:

$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
);

Both arrays can be of varying lengths but each item in $array2 must be applied to an item in $array1. Just throwing this out there to see if anyone has any experience with this sort of merge. My current solution only gives me a result array with a length that equals the length of $array2. Working on this now but any insight would be appreciated!

  • 写回答

1条回答 默认 最新

  • doujie9252 2013-09-03 19:38
    关注

    Like this:

    $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']
        );
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?