简体   繁体   English

循环遍历多维数组

[英]looping through multi dimension array

I am parsing A JSON file in PHP using PHP Decode我正在使用 PHP 解码解析 PHP 中的 JSON 文件

$json= file_get_contents ("resultate.json");


$json = json_decode($json, true);

and then I am trying to loop through the results and display them然后我试图遍历结果并显示它们

 foreach($json as $zh){


$name = $zh[0]["VORLAGEN"][0]["JA_PROZENT"];
$JA = $zh[0]["VORLAGEN"][0]["VORLAGE_NAME"];
$kreis = $zh[0]["NAME"];
$kreis1 = $zh[22]["NAME"];




 echo $name;
 echo $JA;
 echo $kreis;
 echo $kreis1;
    ...}

but I receive only one element eg position 0 or position 22. I would like to receive all the results and display them in a list.但我只收到一个元素,例如 position 0 或 position 22。我想收到所有结果并将它们显示在列表中。 Below you can see the array下面你可以看到数组

Array
(
    [GEBIETE] => Array
        (
            [0] => Array
                (
                    [VORLAGEN] => Array
                        (
                            [0] => Array
                                (
                                    [VORLAGE_NAME] => Kantonale Volksabstimmung über die Vorlage Steuergesetz (StG) (Änderung vom 1. April 2019; Steuervorlage 17)
                                    [JA_STIMMEN_ABSOLUT] => 205
                                    [STIMMBETEILIGUNG] => 28.11
                                    [NEIN_STIMMEN_ABSOLUT] => 183
                                    [VORLAGE_ID] => 2491
                                    [JA_PROZENT] => 52.84
                                )

                        )

                    [NAME] => Aeugst a.A.
                    [WAHLKREIS] => 0
                    [BFS] => 1
                )

            [1] => Array
                (
                    [VORLAGEN] => Array
                        (
                            [0] => Array
                                (
                                    [VORLAGE_NAME] => Kantonale Volksabstimmung über die Vorlage Steuergesetz (StG) (Änderung vom 1. April 2019; Steuervorlage 17)
                                    [JA_STIMMEN_ABSOLUT] => 1000
                                    [STIMMBETEILIGUNG] => 26.15
                                    [NEIN_STIMMEN_ABSOLUT] => 851
                                    [VORLAGE_ID] => 2491
                                    [JA_PROZENT] => 54.02
                                )

                        )

                    [NAME] => Affoltern a.A.
                    [WAHLKREIS] => 0
                    [BFS] => 2
                )

            [2] => Array
                (
                    [VORLAGEN] => Array
                        (
                            [0] => Array
                                (
                                    [VORLAGE_NAME] => Kantonale Volksabstimmung über die Vorlage Steuergesetz (StG) (Änderung vom 1. April 2019; Steuervorlage 17)
                                    [JA_STIMMEN_ABSOLUT] => 661
                                    [STIMMBETEILIGUNG] => 30.98
                                    [NEIN_STIMMEN_ABSOLUT] => 454
                                    [VORLAGE_ID] => 2491
                                    [JA_PROZENT] => 59.28
                                )

                        )

                    [NAME] => Bonstetten
                    [WAHLKREIS] => 0
                    [BFS] => 3
                )

can you please tell me how to print all the elements of this array?你能告诉我如何打印这个数组的所有元素吗?

There is GEBIETE layer in your json, so change foreach($json as $zh) to foreach($json["GEBIETE"] as $zh) will works. json 中有GEBIETE层,因此将foreach($json as $zh)更改为foreach($json["GEBIETE"] as $zh)将起作用。

You can nest multiple foreach :您可以嵌套多个foreach

$root = $json['GEBIETE'];

foreach ($root as $elem) {
    foreach ($elem as $key => $value) {
        // VORLAGEN is an array of its own so if you want to print the keys you should foreach this value as well
        if(is_array($value)) {
            foreach ($value[0] as $k => $v) {
                echo $k."\t".$v;
            }
        }
        else {
            echo $value;
        }
    }
}

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

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