dongyu1125
2015-05-07 18:12
浏览 48
已采纳

PHP:如何将多个关联数组合并为多维数组?

Consider three associative arrays in php:

$a1 = array(
"a" => "1",
"b" => "2",
"c" => "3"
);

$a2 = array(
"d" => "4",
"e" => "5",
"f" => "6"
);

$a3 = array(
"g" => "7",
"h" => "8",
"i" => "9"
);

How would you efficiently combine these into a multidimensional array as follows:

$result = array(
"1" => array("4","7"),
"2" => array("5","8"),
"3" => array("6","9")
);

Thanks in advance!

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

4条回答 默认 最新

  • dongnai6973 2015-05-07 18:21
    已采纳

    Very similar to a couple of questions I answered last night:

    $a1 = array(
    "a" => "1",
    "b" => "2",
    "c" => "3"
    );
    
    $a2 = array(
    "d" => "4",
    "e" => "5",
    "f" => "6"
    );
    
    $a3 = array(
    "g" => "7",
    "h" => "8",
    "i" => "9"
    );
    
    $x = array_combine(
        $a1,
        call_user_func_array('array_map', [null, $a2, $a3])
    );
    var_dump($x);
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dsjpqpdm620596 2015-05-07 18:21

    Fairly straight forward:

    foreach($a1 as $val) {
        $result[$val] = array(array_shift($a2), array_shift($a3));
    }
    
    评论
    解决 无用
    打赏 举报
  • duancong6937 2015-05-07 18:22

    Is this what you mean?

      <?php
    
    $a1 = array(
        "a" => "1",
        "b" => "2",
        "c" => "3"
    );
    
    $a2 = array(
        "d" => "4",
        "e" => "5",
        "f" => "6"
    );
    
    $a3 = array(
        "g" => "7",
        "h" => "8",
        "i" => "9"
    );
    
    $a1_ = array_values($a1);
    $a2_ = array_values($a2);
    $a3_ = array_values($a3);
    
    $newarray = array();
    $max = count($a1_);
    for($i = 0; $i < $max; $i++) {
        $newarray[$a1_[$i]] = array($a2_[$i], $a3_[$i]);
    }
    
    var_dump($newarray);
    

    which outputs

    array(3) {
      [1]=>
      array(2) {
        [0]=>
        string(1) "4"
        [1]=>
        string(1) "7"
      }
      [2]=>
      array(2) {
        [0]=>
        string(1) "5"
        [1]=>
        string(1) "8"
      }
      [3]=>
      array(2) {
        [0]=>
        string(1) "6"
        [1]=>
        string(1) "9"
      }
    }
    
    评论
    解决 无用
    打赏 举报
  • doujugu1722 2015-05-07 18:27

    First combine all of the arrays into a single multi-dimensional array.

    $arrays = array();
    array_push($arrays,$a1) //Do the same for the rest
    

    Create another array and loop through the one we just created.

    $result = array();
    foreach($arrays as $a) {
        $result[$a[0]] = array_shift($a);
    }
    

    This pulls the first value out of the array and makes it the key. It then uses array_shift to pop out the first element of the array so it is not included in the assignment.

    Is this along the lines of what you are looking for?

    Note: This is scaleable for any size array and any number of arrays. Just add any array you want included into $arrays using array_push and it will follow the pattern you outlined above.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题