为了账号安全,请及时绑定邮箱和手机立即绑定

PHP 从 Json 中选择数据

PHP 从 Json 中选择数据

PHP
翻过高山走不出你 2023-09-08 22:07:20
我的 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"            }          ]        }      ]    }  ]}我的PHPforeach($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在此输入图像描述
查看完整描述

2 回答

?
元芳怎么了

TA贡献1798条经验 获得超7个赞

这是你想要的?


<?php


$jsonData = '{

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

            }

          ]

        }

      ]

    }

  ]

}';


$data = json_decode($jsonData);

echo($data->users[0]->authority[0]->mission[0]->permission);


查看完整回答
反对 回复 2023-09-08
?
拉丁的传说

TA贡献1789条经验 获得超8个赞

您对对象和数组感到困惑


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

    $auths = $currentUser->authority;

    foreach($auths as $currentAuth){

        foreach($currentAuth->mission as $permission){

          foreach($permission as $positionLabel => $permissionValue){

              if($permissionValue == "add"){

                  // code here

              }

          }

        }

    }

}

确保您使用好的标签。虚拟占位符可能没问题,但它使跟踪错误变得非常困难。我假设您想检查他们是否拥有使用数据库、使用 PDO 的权限?


// array

$arr = ["this", "is", "an", "array"];

echo $arr[0] // prints out the word this. 

$json = { "attribute": "value"}

echo $json->attribute // prints out the word value.

您可以在 JSON 对象中包含数组,也可以在数组中包含 JSON 对象。您访问它们的方式有所不同。


查看完整回答
反对 回复 2023-09-08
  • 2 回答
  • 0 关注
  • 100 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号