阵列组合保留索引

时间:2013-07-06 20:29:43

标签: php 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');

我希望有一个可以这样使用的功能:

如果可能,还支持更多数组,如第5等......

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

结果通缉:

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

非常感谢你们所有的帮助^ _ ^

3 个答案:

答案 0 :(得分:0)

通常,数组ID的重置是由array_merge PHP函数引起的,而不是使用那个尝试简单地“添加”数组来形成你的合并数组,这里有一个例子,它应该有助于对正在发生的事情有所了解。

所以我们从两个小而简单的数组开始,索引设置为默认值(0)以外的其他数据。

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

使用'普通'array_merge会做类似这样的事情,因为你可以看到两个数组ID都被重置为0和1,而合并前它们分别是1和3。

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

但是,当您将两个数组添加到一起时,您的索引ID将保持不变:

$mergedArray = $array1 + $array2; // will keep the index ID's intact
// $mergedArray = array(1 => 'one', 3 => 'three');

答案 1 :(得分:0)

我使用这个自定义功能。感谢Trey在“array combine three or more arrays with php”处的原始功能。您可以使用此功能添加任意数量的数组。

功能#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

使用的示例数组:

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

功能用途:

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

我添加了两个自动数字索引,并从1个0开始。

示例数组输出:

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

这是另外一个使用自定义子键,在这一个我添加了一个自动名称索引并从1而不是0开始。

功能#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

使用的示例数组:

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

功能用途:

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

示例数组输出:

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

哦,是的,这个函数也支持数组中的空值: - )

我希望这可以帮助其他需要simular数组配置的人。

答案 2 :(得分:0)

这将为您提供所需的准确输出。希望它对你有用。

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