访问和打印嵌套数组

时间:2016-05-07 06:51:39

标签: php mysql

我有以下嵌套数组。

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

我怎样才能做到这一点。在此先感谢您的帮助。

2 个答案:

答案 0 :(得分: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');

)

答案 1 :(得分:0)

使用PDO插入和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);
}}}

没有对此进行测试,而是沿着这些方向进行测试。