dongmei6426 2019-03-21 03:09
浏览 53
已采纳

用PHP组合数组[新]

I have three arrays:

$a = (a1, a2, a3, a4, a5, a6, a7, a8, a9, a10);
$b = (b1, b2, b3, b4, b5, b6, b7, b8, b9, b10);
$c = (c1, c2, c3, c4, c5, c6, c7, c8, c9, c10);

I want to combine these arrays to create:

$new1 = (a1, a2, b1, b2, c1, c2);

$new2 = (a3, a4, b3, b4, c3, c4);

$new3 = (a5, a6, b5, b6, c5, c6);

$new4 = (a7, a8, b7, b8, c7, c8);

$new5 = (a9, a10, b9, b10, c9, c10);

How to make it like that?

  • 写回答

2条回答 默认 最新

  • duanhu7400 2019-03-21 03:29
    关注

    You, you would want to map each array into it's chunked version using array_map and array_chunk, so that you get them in 2 element arrays. Then, you would want to reduce those so that each chunk goes in the same final array, for which you would use array_reduce:

    $a = array('a1', 'a2', 'a3', 'a4', 'a5', 'a6', 'a7', 'a8', 'a9', 'a10');
    $b = array('b1', 'b2', 'b3', 'b4', 'b5', 'b6', 'b7', 'b8', 'b9', 'b10');
    $c = array('c1', 'c2', 'c3', 'c4', 'c5', 'c6', 'c7', 'c8', 'c9', 'c10');
    
    $final = array_reduce(array_map(function($el) {
                 return array_chunk($el, 2);
             }, [$a, $b, $c]), function($carry, $item) {
                 foreach($item as $key => $value) {
                     if (!isset($carry[$key])) {
                         $carry[$key] = [];
                     }
                     $carry[$key] = array_merge($carry[$key], $value);
                 }
                 return $carry;
             });
    
    $new1 = $f[0];
    $new2 = $f[1];
    $new3 = $f[2];
    $new4 = $f[3];
    $new5 = $f[4];
    
    var_dump($f);
    /*
     array(5) {
      [0]=>
      array(6) {
        [0]=>
        string(2) "a1"
        [1]=>
        string(2) "a2"
        [2]=>
        string(2) "b1"
        [3]=>
        string(2) "b2"
        [4]=>
        string(2) "c1"
        [5]=>
        string(2) "c2"
      }
      [1]=>
      array(6) {
        [0]=>
        string(2) "a3"
        [1]=>
        string(2) "a4"
        [2]=>
        string(2) "b3"
        [3]=>
        string(2) "b4"
        [4]=>
        string(2) "c3"
        [5]=>
        string(2) "c4"
      }
      [2]=>
      array(6) {
        [0]=>
        string(2) "a5"
        [1]=>
        string(2) "a6"
        [2]=>
        string(2) "b5"
        [3]=>
        string(2) "b6"
        [4]=>
        string(2) "c5"
        [5]=>
        string(2) "c6"
      }
      [3]=>
      array(6) {
        [0]=>
        string(2) "a7"
        [1]=>
        string(2) "a8"
        [2]=>
        string(2) "b7"
        [3]=>
        string(2) "b8"
        [4]=>
        string(2) "c7"
        [5]=>
        string(2) "c8"
      }
      [4]=>
      array(6) {
        [0]=>
        string(2) "a9"
        [1]=>
        string(3) "a10"
        [2]=>
        string(2) "b9"
        [3]=>
        string(3) "b10"
        [4]=>
        string(2) "c9"
        [5]=>
        string(3) "c10"
      }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 爬虫爬取网站的一些信息
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题
  • ¥15 Utunbu中vscode下cern root工作台中写的程序root的头文件无法包含
  • ¥15 麒麟V10桌面版SP1如何配置bonding
  • ¥15 Marscode IDE 如何预览新建的 HTML 文件
  • ¥15 K8S部署二进制集群过程中calico一直报错