Merge multiple arrays into one 2d array with associative rows

2.9k views Asked by At

This is what I'm trying to arrive at:

array (
  'category_0' => 
  array (
    'count' => 12,
    'avg' => 11,
  ),
  'category_1' => 
  array (
    'count' => 14,
    'avg' => 13,
  ),
  'category_2' => 
  array (
    'count' => 16,
    'avg' => 15,
  ),
  'category_3' => 
  array (
    'count' => 18,
    'avg' => 17,
  ),
)

From the following:

$category = 
array (
  0 => '9',
  1 => '10',
  2 => '11',
  3 => '12',
)

and:

$count =
array (
  0 => '12',
  1 => '14',
  2 => '16',
  3 => '18',
)

and:

$avg = 
array (
  0 => '11',
  1 => '13',
  2 => '15',
  3 => '17',
)

I've arrived at the $count and $avg variables by performing some other methods using a foreach on the $category and the array_map function. Now I need to rejoin everything per the intial example. I've tried array_merge() but am unable to get it to work correctly. The key name of 'category_x' is unimportant. I'm not even sure if it's possible to get the final result in the order I need it. Any help would be appreciated.

3

There are 3 answers

0
Francois Deschenes On BEST ANSWER

Using a combination of array_keys and a foreach loop, you can easily accomplish this.

Example:

<?php

$category = array (
    0 => '9',
    1 => '10',
    2 => '11',
    3 => '12'
);
$count = array (
    0 => '12',
    1 => '14',
    2 => '16',
    3 => '18'
);
$avg = array (
    0 => '11',
    1 => '13',
    2 => '15',
    3 => '17'
);

$out = array();
$key = array_keys($category);
foreach ($key as $key) {
    $out["category_{$key}"] = array(
        'count' => isset($count[$key]) ? $count[$key] : 0,
        'avg' => isset($avg[$key]) ? $avg[$key] : 0
    );
}
print_r($out);

Output:

Array
(
    [category_0] => Array
        (
            [count] => 12
            [avg] => 11
        )

    [category_1] => Array
        (
            [count] => 14
            [avg] => 13
        )

    [category_2] => Array
        (
            [count] => 16
            [avg] => 15
        )

    [category_3] => Array
        (
            [count] => 18
            [avg] => 17
        )

)
0
Ben D On

I might be misunderstanding, but I think you're over-thinking this. Just use a for loop to build a master array:

<?php
$category = array (
  0 => '9',
  1 => '10',
  2 => '11',
  3 => '12'
);


$count = array (
  0 => '12',
  1 => '14',
  2 => '16',
  3 => '18'
);

$avg =  array (
  0 => '11',
  1 => '13',
  2 => '15',
  3 => '17'
);

$final_array = array();
for($i=0;$i<count($category);$i++){
    $final_array['category_'.$i] = array(
        'count' =>  $count[$i],
        'avg'   =>  $avg[i]
    );
}

print_r($final_array);

?>

Also, keep in mind that your code will throw a syntax error because the last element cannot be followed by a comma.

0
cwallenpoole On

There is no way to do anything like that with a function call, you will need to loop manually. If this could cause a problem because of the number of records, then you may wish to re-think and refactor.

$res = array();

// category values are irrelevant, but it is easier than using array_keys
// (and never use for loops when you can use foreach: foreach can be 20% faster
foreach($category as $key=>$val)
{
    // use manual concatenation and it is generally about 1.5% faster
    // and clearer to your compatriots
    $res['Category_'.$key] = array(
          // since key maps to the same key for all, then simply use that 
          // for the lookup
          /*
            if you KNOW that there will always be a key in $count and $avg
            then use the following:
          */ 
          'count' => $count[$key],
          'avg' => $avg[$key]
           /*
             otherwise you will need to use this:
          'count' => isset($count[$key])?$count[$key]:0,
          'avg' => isset($avg[$key])?$avg[$key]:0
           */
    );
}
// $res now has everything you're looking for.