如何使用array-index访问数组元素值?
<?
$json = '{
"dynamic":{
"pageCount":"12",
"tableCount":"1"
}
}';
$arr = json_decode($json, true);
echo $arr['dynamic']['pageCount']; // working
echo $arr[0]['pageCount']; // not working
?>
我不知道'动态'中有什么,所以我想动态访问pageCount值?
答案 0 :(得分:11)
array_values是您正在寻找的功能
示例:
<?php
$json = '{
"dynamic":{
"pageCount":"12",
"tableCount":"1"
}
}';
$arr = json_decode($json, true);
echo $arr['dynamic']['pageCount']; // working
$arr = array_values($arr);
echo $arr[0]['pageCount']; // NOW working
?>
答案 1 :(得分:1)
$arr = json_decode($json, true);
foreach ($arr as $key => $value) {
if (isset($value['pageCount'])) {
//do something with the page count
}
}
如果结构总是一个嵌套的JS对象:
$obj = current($arr);
echo $obj['pageCount'];