PHP:如何使用array-index访问数组元素值

Shi*_*hiv 1 php arrays

如何使用array-index访问数组元素值?

<?
$json = '{
    "dynamic":{
       "pageCount":"12",
       "tableCount":"1"
    }
}';

$arr = json_decode($json, true);

echo $arr['dynamic']['pageCount']; // working
echo $arr[0]['pageCount']; // not working
?>
Run Code Online (Sandbox Code Playgroud)

我不知道'动态'中有什么,所以我想动态访问pageCount值?

Tuf*_*rım 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

?>
Run Code Online (Sandbox Code Playgroud)