-1

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!

Stack
  • 348
  • 3
  • 17

4 Answers4

1

Fairly straight forward:

foreach($a1 as $val) {
    $result[$val] = array(array_shift($a2), array_shift($a3));
}
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
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);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
  • Well I prepared the exact same answer, just with an explanation what the code does and now I was waiting for OP to show some effort and some work! But now OP got his candy! – Rizier123 May 07 '15 at 18:26
  • Perfect! I wish I could award this one to you now! Failed on my server at first due to the old php install. After I accepted the answer, I corrected it to call_user_func_array('array_map', array(null, $a2, $a3)) and it runs great now. It's shorter than what I accepted. – Stack May 07 '15 at 19:05
  • @MarkBaker http://stackoverflow.com/q/30131528 Do you see more, less or equal amount of effort in OP's new question (compared to this)? Also since he posted this question in this quality he probably assumes that he will get his code with this effort. And exactly this behaviour comes from answering questions like this! (with no effort shown) – Rizier123 May 08 '15 at 20:01
0

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"
  }
}
fbas
  • 1,676
  • 3
  • 16
  • 26
  • Works, but I have to deal with 30-50 arrays - having to declare $a1_ = array_values($a1); $a2_ = array_values($a2); $a3_ = array_values($a3); seems like a drag. – Stack May 07 '15 at 18:52
0

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.

Kirk Logan
  • 733
  • 2
  • 8
  • 23