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

时间:2022-03-11 12:56:30

Consider three associative arrays in php:

考虑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 个解决方案

#1


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

#2


Fairly straight forward:

非常坦率的:

foreach($a1 as $val) {
    $result[$val] = array(array_shift($a2), array_shift($a3));
}

#3


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

#4


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.

这会将第一个值从数组中拉出来并使其成为关键值。然后使用array_shift弹出数组的第一个元素,使其不包含在赋值中。

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.

注意:对于任何大小的数组和任意数量的数组,这都是可扩展的。只需使用array_push将您想要包含的任何数组添加到$ arrays中,它将遵循您在上面概述的模式。

#1


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

#2


Fairly straight forward:

非常坦率的:

foreach($a1 as $val) {
    $result[$val] = array(array_shift($a2), array_shift($a3));
}

#3


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

#4


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.

这会将第一个值从数组中拉出来并使其成为关键值。然后使用array_shift弹出数组的第一个元素,使其不包含在赋值中。

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.

注意:对于任何大小的数组和任意数量的数组,这都是可扩展的。只需使用array_push将您想要包含的任何数组添加到$ arrays中,它将遵循您在上面概述的模式。