2015-12-04 90 views
0

我正在尋找一個解決方案來建立從foreach循環的平板陣列數據的關聯數組:展望創建關聯數組

我有什麼是對第一行和頭一個CSV/xls文件數據在下一行。

ROW1:頭 Row2,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" 
      } 
     } 
+0

你嘗試過什麼? – Burki

回答

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

您只需從陣列中刪除第一項(標題行):

array_splice($yourArray, 0, 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); 

OUTPUT:

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" 
    } 
}