1

I'm trying to combine four array's into one while preserving index numbers of the whole array.

Example Arrays:

$codes = array('US', 'GB', 'AE');
$names = array('United States', 'United Kingdom', 'United Arab Emirates');
$population = array('307,212,123', '61,113,205', '4,798,491');
$area = array('9,826,675', '243,610', '83,600');

I would like to have a function that could be used like this:

Also if possible, support for more arrays like 5th etc...

$country = combineArray($codes, $names, $population, $area);

Result Wanted:

    [country] => Array
      (
        [0] => Array
          (
            [0] => US
            [1] => United States
            [2] => 307,212,123
            [3] => 9,826,675
          )
        [1] => Array
          (
            [0] => GB
            [1] => United Kingdom
            [2] => 61,113,205
            [3] => 243,610
          )
        [2] => Array
          (
            [0] => AE
            [1] => United Arab Emirates
            [2] => 4,798,491
            [3] => 83,600
          )
      )

Thank you so much for all the help ^_^

Tux
  • 247
  • 2
  • 14

3 Answers3

0

Normally the reset of array ID's is caused by the array_merge PHP function, instead of using that try simply 'adding' the arrays to form your merged array, here's an example which should help provide some understanding on what's going on.

So we start with two small and simple arrays with indexes set to something other than the default (0).

$array1 = array(1 => 'one');
$array2 = array(3 => 'three');

Using a 'normal' array_merge would do something like this, as you can see both array ID's have been reset to 0 and 1, where as they were 1 and 3 respectively before the merge.

$mergedArray = array_merge($array1, $array2);
// $mergedArray = array(0 => 'one', 1 => 'three');

However when you add the two arrays together your index ID's will remain untouched:

$mergedArray = $array1 + $array2; // will keep the index ID's intact
// $mergedArray = array(1 => 'one', 3 => 'three');
iTom
  • 281
  • 1
  • 5
  • This won't work, because its not combining the array as shown in example. I've already tried this aproach. – Tux Jul 06 '13 at 21:07
0

I got this working with this customized function. Thanks to Trey for original function at " array combine three or more arrays with php " You can add as many arrays as you like with this functions.

Function #1:

function arrayMerge() {
    $args = func_get_args();
    $idx1 = range(1, count($args[1]));
    $idx2 = range(1, count($args[2]));
    foreach($args as $key => $array) {
        $vkey = array_shift($idx2);
        foreach($array as $akey => $val) {
            $result[$idx1[$akey]][$vkey] = $val;
        } #foreach
    } #foreach
    return $result;
} #arrayMerge

Example Array Used:

$codes = array('US', 'GB', 'AE');
$names = array('United States', 'United Kingdom', 'United Arab Emirates');
$population = array('307,212,123', '61,113,205', '4,798,491');
$area = array('9,826,675', '243,610', '83,600');

Function Usage:

print_r( arrayMerge($codes, $names, $population, $area) );

I've added two auto numeric index's and started from 1 instad of 0.

Example Array Output:

Array
  (
    [1] => Array
      (
        [1] => US
        [2] => United States
        [3] => 307,212,123
        [4] => 9,826,675
      )
    [2] => Array
      (
        [1] => GB
        [2] => United Kingdom
        [3] => 61,113,205
        [4] => 243,610
      )
    [3] => Array
      (
        [1] => AE
        [2] => United Arab Emirates
        [3] => 4,798,491
        [4] => 83,600
      )
  )

Here is one more that uses custom sub-keys, in this one I've added one auto nameric index and started from 1 instead of 0.

Function #2:

function arrayMerge2() {
    $args = func_get_args();
    $idx = range(1, count($args[1]));
    $keys = array_shift($args);
    foreach($args as $key => $array) {
        $vkey = array_shift($keys);
        foreach($array as $akey => $val) {
            $result[$idx[$akey]][$vkey] = $val;
        } #foreach
    } #foreach
    return $result;
} #arrayMerge2

Example Array Used:

$codes = array('US', 'GB', 'AE');
$names = array('United States', 'United Kingdom', 'United Arab Emirates');
$population = array('307,212,123', '61,113,205', '4,798,491');
$area = array('9,826,675', '243,610', '83,600');
$keys = array('code', 'name', 'popu', 'area');

Function Usage:

print_r( arrayMerge2($keys, $codes, $names, $population, $area) );

Example Array Output:

Array
  (
    [1] => Array
      (
        [code] => US
        [name] => United States
        [popu] => 307,212,123
        [area] => 9,826,675
      )
    [2] => Array
      (
        [code] => GB
        [name] => United Kingdom
        [popu] => 61,113,205
        [area] => 243,610
      )
    [3] => Array
      (
        [code] => AE
        [name] => United Arab Emirates
        [popu] => 4,798,491
        [area] => 83,600
      )
  )

Oh yeah, this functions also support empty values in array :-)

I hope this helps some one else that needs simular array config.

Community
  • 1
  • 1
Tux
  • 247
  • 2
  • 14
0

this will give you exact output what you need. Hope it will useful to you.

<?php
$codes = array('US', 'GB', 'AE');
$names = array('United States', 'United Kingdom', 'United Arab Emirates');
$population = array('307,212,123', '61,113,205', '4,798,491');
$area = array('9,826,675', '243,610', '83,600');

$country = array();
$size= sizeof($codes); //This will work only if all array size are same.

for($i=0;$i<$size;$i++)
{
    $country[$i][]=$codes[$i];
    $country[$i][]=$names[$i];
    $country[$i][]=$population[$i];
    $country[$i][]=$area[$i];
}
echo "<pre>";
print_r($country);
Kashyap
  • 381
  • 4
  • 10