Tek
Tek

Reputation: 3070

Combine arrays to form multidimensional array in php

I know there's a ton of answers but I can't seem to get it right. I have the following arrays and what I've tried:

$a = array ( 0 => '1421' , 1 => '2241' );
$b = array ( 0 => 'teststring1' , 1 => 'teststring2' );
$c = array ( 0 => 'teststring3' , 1 => 'teststring4' );
$d = array ( 0 => 'teststring5' , 1 => 'teststring6' );

$e = array_combine($a, array($b,$c,$d) );

But with this I get the error array_combine() [function.array-combine]: Both parameters should have an equal number of elements.

I know it's because the $a's array values aren't keys. That's why I'm coming here to see if I could get some help with an answer that can help me make it look something like this:

array(2) {

  [1421]=>array( [0] => teststring1
                 [1] => teststring3
                 [2] => teststring5
                )

  [2241]=>array( [0] => teststring2
                 [1] => teststring4
                 [2] => teststring6
               )


}

Upvotes: 6

Views: 19875

Answers (3)

mickmackusa
mickmackusa

Reputation: 48100

Here is a one-liner in a functional coding style. Calling array_map() with a null function parameter followed by the "values" arrays will generate the desired subarray structures. array_combine() does the key=>value associations.

Code (Demo)

var_export(array_combine($a, array_map(null, $b, $c, $d)));

Output:

array (
  1421 => 
  array (
    0 => 'teststring1',
    1 => 'teststring3',
    2 => 'teststring5',
  ),
  2241 => 
  array (
    0 => 'teststring2',
    1 => 'teststring4',
    2 => 'teststring6',
  ),
)

Super clean, right? I know. It's a useful little trick when you don't have control of the initial array generation step.

Upvotes: 1

pinaki
pinaki

Reputation: 5473

Here's a new version of array_merge_recursive which will handle integer keys. Let know how it goes.

$a = array ( 0 => '1421' , 1 => '2241' );
$b = array ( 0 => 'teststring1' , 1 => 'teststring2' );
$c = array ( 0 => 'teststring3' , 1 => 'teststring4' );
$d = array ( 0 => 'teststring5' , 1 => 'teststring6' );

$e = array_combine($a, array_merge_recursive2($b, $c, $d));
echo "<pre>";
print_r($e);



function array_merge_recursive2() {
    $args = func_get_args();
    $ret = array();

    foreach ($args as $arr) {
        if(is_array($arr)) {
            foreach ($arr as $key => $val) {
                $ret[$key][] = $val;
            }
        }
    }
    return $ret;
}

Upvotes: 0

Felix Kling
Felix Kling

Reputation: 817238

If you have control over creating the arrays, you should create them like:

$a = array ('1421' ,'2241');
$b = array ('teststring1', 'teststring3', 'teststring5');
$c = array ('teststring2', 'teststring4', 'teststring6');

$e = array_combine($a, array($b,$c) );

If not, you have to loop over them:

$result = array();
$values = array($b, $c, $d);

foreach($a as $index => $key) {
    $t = array();
    foreach($values as $value) {
        $t[] = $value[$index];
    }
    $result[$key]  = $t;
}

DEMO

Upvotes: 3

Related Questions