PHP 从 Json 中选择数据

我的 JSON 数据


{

  "users": [

    {

      "userid": 1,

      "username": "Admin",

      "usermail": "admin@admin.com",

      "authority": [

        {

          "authorityid": 1,

          "authoritytext": "Admin",

          "mission": [

            {

              "permission": "add"

            },

            {

              "permission": "delete"

            },

            {

              "permission": "move"

            }

          ]

        },

        {

          "authorityid": 1,

          "authoritytext": "Super Admin",

          "mission": [

            {

              "permission": "no add"

            },

            {

              "permission": "no delete"

            },

            {

              "permission": "only moderate"

            }

          ]

        }

      ]

    }

  ]

}

我的PHP


foreach($result->users as $ok)  {


     foreach($ok->authority as $okey) {


         foreach($okey->mission as $okeyokey) {


            $test = $okeyokey->permission;

            echo $test;


         }

     }

 }

怎么做这个?我想仅显示解析 json 权限{0} -> misson{0} 显示“权限”“添加”请帮助我。也许看看 ScreenShot >>>>> 我想要过滤器 {0}{1}{2} 并选择 0 -> 显示解析 json

在此输入图像描述


翻过高山走不出你
浏览 101回答 2
2回答

元芳怎么了

这是你想要的?<?php$jsonData = '{&nbsp; "users": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; "userid": 1,&nbsp; &nbsp; &nbsp; "username": "Admin",&nbsp; &nbsp; &nbsp; "usermail": "admin@admin.com",&nbsp; &nbsp; &nbsp; "authority": [&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "authorityid": 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "authoritytext": "Admin",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "mission": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "permission": "add"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "permission": "delete"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "permission": "move"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "authorityid": 1,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "authoritytext": "Super Admin",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "mission": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "permission": "no add"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "permission": "no delete"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "permission": "only moderate"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }&nbsp; ]}';$data = json_decode($jsonData);echo($data->users[0]->authority[0]->mission[0]->permission);

拉丁的传说

您对对象和数组感到困惑foreach($result->users as $currentUser){&nbsp; &nbsp; $auths = $currentUser->authority;&nbsp; &nbsp; foreach($auths as $currentAuth){&nbsp; &nbsp; &nbsp; &nbsp; foreach($currentAuth->mission as $permission){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($permission as $positionLabel => $permissionValue){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($permissionValue == "add"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // code here&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}确保您使用好的标签。虚拟占位符可能没问题,但它使跟踪错误变得非常困难。我假设您想检查他们是否拥有使用数据库、使用 PDO 的权限?// array$arr = ["this", "is", "an", "array"];echo $arr[0] // prints out the word this.&nbsp;$json = { "attribute": "value"}echo $json->attribute // prints out the word value.您可以在 JSON 对象中包含数组,也可以在数组中包含 JSON 对象。您访问它们的方式有所不同。
打开App,查看更多内容
随时随地看视频慕课网APP