简体   繁体   English

访问和打印嵌套数组

[英]Accessing and printing nested array

I have the following nested array. 我有以下嵌套数组。

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

I want to process the above array and build an insert query using foreach loop as follows: 我想处理上面的数组并使用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');

How can I achieve this. 我怎样才能做到这一点。 Thanks in advance for the help. 先谢谢您的帮助。

You need to loop every part of your array. 您需要遍历数组的每个部分。 Just use foreach 3 time. 只需使用foreach 3次。 store the query in an array. 将查询存储在数组中。 Let your array is $arr . 让你的数组是$arr

Your array: 你的阵列:

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

Mechanism / Technique: 机制/技术:

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

Result 结果

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

)

Use PDO for inserting and foreach to loop through all the values in multi array. 使用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);
}}}

Have not tested this but something along these lines. 没有测试过这个但是沿着这些方向的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM