期待创建一个关联数组

时间:2015-12-04 09:05:36

标签: php arrays

我正在寻找一种解决方案,从foreach循环中的平面数组数据创建一个关联数组:

我所拥有的是一个csv / xls文件,它在第一行有标题,在下一行有数据。

Row1:标题 第2,3,4,5行:数据

数组看起来:

array(3) {
          [0]=>
          array(7) {
            [0]=>
            string(3) "country"
            [1]=>
            string(7) "state"
            [2]=>
            string(3) "city"
            [3]=>
            string(5) "name"
            [4]=>
            string(4) "address"
            [5]=>
            string(6) "gender"
            [6]=>
            string(6) "status"
          }
          [1]=>
          array(7) {
            [0]=> 
            string(12) "Argentina"
            [1]=>
            string(12) "Corrientes"
            [2]=>
            string(12) "Corrientes"
            [3]=>
            string(12) "Jorge"
            [4]=>
            string(12) "Avenida Avellaneda 12"
            [5]=>
            string(12) "Masculino"
            [6]=>
            string(12) "Activo"
          }
          [2]=>
          array(7) {
            [0]=> 
            string(12) "Argentina"
            [1]=>
            string(12) "Chaco"
            [2]=>
            string(12) "Resistencia"
            [3]=>
            string(12) "Mariano"
            [4]=>
            string(12) "Avenida Peron 12"
            [5]=>
            string(12) "Masculino"
            [6]=>
            string(12) "Activo"
          }
        }

我需要得到的结果是:

array(2) {
          [0]=>
          array(7) {
            ['country']=> 
            string(12) "Argentina"
            ['state']=>
            string(12) "Corrientes"
            ['city']=>
            string(12) "Corrientes"
            ['name']=>
            string(12) "Jorge"
            ['address']=>
            string(12) "Avenida Avellaneda 12"
            ['gender']=>
            string(12) "Masculino"
            ['status']=>
            string(12) "Activo"
          }
          [1]=>
          array(7) {
            ['country']=> 
            string(12) "Argentina"
            ['state']=>
            string(12) "Chaco"
            ['city']=>
            string(12) "Resistencia"
            ['name']=>
            string(12) "Mariano"
            ['address']=>
            string(12) "Avenida Peron 12"
            ['gender']=>
            string(12) "Masculino"
            ['status']=>
            string(12) "Activo"
          }
        }

3 个答案:

答案 0 :(得分:1)

$array = $your_flat_array;

for ($i = 1; $i < count($array); $i++) {

    $new_array[$i-1] = [];

    foreach ($array[$i] as $key => $value) {

        $new_array[$i-1][$array[0][$key]] = $value;
    }
}

print_r($new_array);

答案 1 :(得分:1)

  

从平面数组

创建一个多维数组

你已经有了一个多维数组,因为你有一个数组中的数组。

在这种特定情况下,您可以将array_splice()array_combine()结合使用。

试试这个:

$oldArray = array(
    array( "country", "state", "city", "name" ),
    array( "Argentina", "Corrientes", "Corrientes", "Jorge" ),
    array( "Argentina", "Chaco", "Resistencia", "Mariano" )
);

$newArray = array_splice( $oldArray, 1 );

foreach( $newArray as $index => $array ) {
    $newArray[$index] = array_combine( $oldArray[0], $array );
}

echo "<pre>";
var_dump( $newArray );

<强>输出:

array(2) {
  [0]=>
  array(4) {
    ["country"]=>
    string(9) "Argentina"
    ["state"]=>
    string(10) "Corrientes"
    ["city"]=>
    string(10) "Corrientes"
    ["name"]=>
    string(5) "Jorge"
  }
  [1]=>
  array(4) {
    ["country"]=>
    string(9) "Argentina"
    ["state"]=>
    string(5) "Chaco"
    ["city"]=>
    string(11) "Resistencia"
    ["name"]=>
    string(7) "Mariano"
  }
}

答案 2 :(得分:0)

您所要做的就是从数组中删除第一项(标题行):

 array_splice($yourArray, 0, 1);