我正在尝试从关联数组内的关联数组中获取数据。 data像这样:
{
"keys": [
{
"kid": "28f5813e327ad14caaf1bf2a12368587e882b604",
"e": "AQAB",
"kty": "RSA",
"alg": "RS256",
"n": "y-Ssr1zlqWaldUHklobfrJLZkkBYrLVKTOk9SnzkG3v2LPB-0lxLQjm8UDpdZRErn4_FfCQ6b7SAObUp2XgcD-fwmYfq34mvY-xGcvlEarcOFdVD9cwkGBdXL_VJYzqLtvuAL30mbD2TGhIp-QCV-rqb6ujh75vSmwWH1Kkx1HNkVbXHHETvX7h8kj3zmCtRdMGeQJ2YrcJHz3GnJx0M2Zpq1HiAXkYL9GUYsq9maONfsrSeACu7T0W4v-XTppsV3DwU89D_eFd8gJsONvFZPvbqyGivdXYTnYNo7Jf01IisO4JZZrNgUlVT-gPkTM1O1UXevgFY62Br8mBot6Lw6Q",
"use": "sig"
},
{
"alg": "RS256",
"n": "timkjBhJ0F7fgr5-ySitSoSNmUqYcVKgWaUd52HUYPowNwdw1vOWYHuSVol47ssOOaF7dRjgoVHyo_qNgy7rdlU0pUidiYTB6lwSAQYyvk6WAipkpzWH8cr875BMUREyN5aEy-iKsYTB3HeT-gEnLI697eETZtSB8rwlDvyRy7l0wD1GVj4SKTd4P2a2qNCgCfkZzzKqPgmIrPtwkEZb43Cz-A7AfwyXxrMljTkghKkp4zkFRtXplIGjC5LcPZRLSseTYwHP2pV4AtE5KzYxDmtDmY6RyZaMZc_WXNvKBFcO3Rypo4F63lE2x5f7EIbpATWydXq3CMLitLsPor22ow",
"use": "sig",
"kid": "2c3fac16b73fc848d426d5a225ac82bc1c02aefd",
"e": "AQAB",
"kty": "RSA"
}
]
}
例如,要获取kid
,我必须先做两个foreach()
,然后才能获取数据:
foreach ($certs as $key => $val) {
foreach($val AS $key2 => $val2){
$kid = $val2->kid;
}
}
是否有更快或更标准的方式来获取数据?
答案 0 :(得分:2)
您可以使用array_column
从数组中获取kid
。首先,您需要将JSON
解码为array
,然后将其用作
$arr = json_decode($json, true);
print_r(array_column($arr['keys'],'kid'));
答案 1 :(得分:1)
尝试此代码,
$json = json_decode($certs);
foreach ($json->keys as $key => $value) {
$kid= $value->kid;
}
仅使用一个foreach()即可轻松获得孩子的价值。