2016-05-07 92 views
1

我有以下嵌套數組。訪問和打印嵌套數組

Array 
(
    [animals] => Array 
     (
      [carnivores] => Array 
       (
        [tiger] => Array 
         (
          [eyes] => 2 
          [legs] => 4        
         ) 

        [lion] => Array 
         (
          [eyes] => 2 
          [legs] => 4        
         )   
       ) 

      [herbivores] => Array 
       (
        [deer] => Array 
         (
          [eyes] => 2 
          [legs] => 4        
         ) 

        [elephant] => Array 
         (
          [eyes] => 2 
          [legs] => 4 
         ) 
       ) 
     ) 
) 

我要處理上述陣列和使用foreach循環如下建立一個INSERT查詢:

INSERT INTO `abc` (column1,column2,column3,column4, column5) 
     VALUES ('animals','carnivores','tiger','2','4'); 
. 
. 
. 
. 
INSERT INTO `abc` (column1,column2,column3,column4, column5) 
     VALUES ('animals','herbivores','elephant','2','4'); 

我怎樣才能做到這一點。先謝謝您的幫助。

+0

編寫一個遞歸遍歷數組的函數,並在迭代時將所有的鍵集合到一個數組中,然後您可以在查詢中使用它。 – Rizier123

+0

是的,你可以做到這一點,我們一個循環取內部數組的值。 –

+0

爲什麼''獅子''和'[鹿]'鍵被處理時遺漏? – RomanPerekhrest

回答

0

您需要循環數組的每個部分。只需使用foreach 3次。將查詢存儲在數組中。讓你的數組是$arr

你的陣列:

$arr = array(
    "animals" => array 
     (
      "carnivores" => array 
       (
        "tiger" => array 
         (
          "eyes" => '2', 
          "legs" => '4' 
         ), 

        "lion" => Array 
         (
          "eyes" => '2', 
          "legs" => '4' 
         )   
       ), 

      "herbivores" => array 
       (
        "deer" => array 
         (
          "eyes" => '2', 
          "legs" => '4' 
         ), 

        "elephant" => array 
         (
          "eyes" => '2', 
          "legs" => '4' 
         ) 
       ) 
     ) 
); 

機制/技術:

foreach($arr as $key => $value){ 
    $str = "INSERT INTO `abc` (column1,column2,column3,column4, column5) VALUES("; 
    $qryArr = array(); 
    foreach($value as $key2 => $value2){ 
     foreach($value2 as $key3 => $value3){ 
      $eyes = $value3['eyes']; 
      $legs = $value3['legs']; 

      $qryArr[] = $str."'$key', '$key2', '$key3', '$eyes', '$legs');"; 
     }   
    } 
} 

print_r($qryArr); 

結果

Array 
(
    [0] => INSERT INTO `abc` (column1,column2,column3,column4, column5) VALUES('animals', 'carnivores', 'tiger', '2', '4'); 

    [1] => INSERT INTO `abc` (column1,column2,column3,column4, column5) VALUES('animals', 'carnivores', 'lion', '2', '4'); 

    [2] => INSERT INTO `abc` (column1,column2,column3,column4, column5) VALUES('animals', 'herbivores', 'deer', '2', '4'); 

    [3] => INSERT INTO `abc` (column1,column2,column3,column4, column5) VALUES('animals', 'herbivores', 'elephant', '2', '4'); 

) 
0

使用PDO的inser ting和foreach遍歷多個數組中的所有值。

$sql = 'INSERT INTO `abc` (...) VALUES (?,?,?,?,?)'; 
$stmt = $conn->prepare($sql); 
$data = []; 
foreach ($animals as $key_animals => $animalGroup) { 
    foreach ($animalGroup as $key_animalGroup => $animal) { 
     foreach ($animal as $key_animal => $animalProps) { 
      $data = [$key_animals, $key_animalGroup, $key_animal, $animalProps['eyes'], $animalProps['legs']]; 
      $stmt->execute($data); 
}}} 

還沒有測試過這個,但沿着這些線。