从PHP中获取Json数组中的值

时间:2015-11-29 14:27:16

标签: php arrays json

我正在尝试从PHP变量(名为putchar(x);)中的以下JSON数组中获取值。

这是数组的$result

var_dump

我需要在使用array(1) { [272]=> array(1) { [0]=> array(13) { ["actions"]=> array(0) { } ["idle_timeout"]=> int(0) ["cookie"]=> int(0) ["packet_count"]=> int(0) ["hard_timeout"]=> int(0) ["byte_count"]=> int(0) ["length"]=> int(80) ["duration_nsec"]=> int(620000000) ["priority"]=> int(10) ["duration_sec"]=> int(4341) ["table_id"]=> int(0) ["flags"]=> int(0) ["match"]=> array(4) { ["dl_type"]=> int(2048) ["nw_proto"]=> int(1) ["nw_src"]=> string(11) "192.168.1.1" ["nw_dst"]=> string(11) "192.168.1.2" } } } } ["nw_proto"]后获取["nw_src"]["nw_dst"]["priority"]json_decode($result[0])的值,我得到了echo

有人可以帮助我摆脱这个数组结构,它真的让我感到困惑......

2 个答案:

答案 0 :(得分:0)

使用$ result [272] [0]来获得所需的值。

如果你总是需要$ result数组中的第一个元素(在本例中为key 272),你也可以使用reset($ result)。

答案 1 :(得分:0)

您可以这样做:

$priority = $result[272][0]['priority'];  // priority
$nw_proto = $result[272][0]['match']['nw_proto'];  // nw_proto
$nw_src = $result[272][0]['match']['nw_src'];  // nw_src
$nw_dst = $result[272][0]['match']['nw_dst'];  // nw_dst