Get values from Json array in PHP

I'm trying to get the value from this following JSON array in a PHP variable(named $result).

This is a var_dump of the array:

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

and I need to get the value from ["nw_proto"], ["nw_src"], ["nw_dst"] and ["priority"], after I use json_decode($result[0]) and echo, i got a NULL(array?)

could someone help me out of this array structure, it really confuses me...

Answers


You can do something like this:

$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

use $result[272][0] to get to the values you need.

You could also use reset($result) if you always need the first element (in this case key 272) from the $result array.


Need Your Help

How to right code in Django and AngularJS?

python angularjs django

Now I am working with Django on server side and jQuery on client. Django views functions return templates with js code.

MYSQL Trigger to update table that is based on two other tables

mysql database database-design triggers

I've created a table name 'combined_data' using data from two tables 'store_data' and 'hd_data'. The two tables share a common column which I used to link the data when creating the new table and t...