Ppp
Ppp

Reputation: 85

Get data from a multidimensional array

I have the following array (exposed using var_dump).

array (size=3)
    'auth' => string 'debug' (length=5)
    'url' => string 'http://X.X.X.X/status.cgi?' (length=31)
    'page' => string '{ "host": {   "uptime": 1543,     "time": "2011-07-26 12:07:40",  "fwversion": "v1.1.1",  "hostname": "ASDASDASD",    "netrole": "DFDFDF"  },  "lan": {   "status": [{ "plugged": 1, "speed": 100, "duplex": 1 }],    "hwaddr": "00:00:22:11:11:11",  "ip": "",   "rx": { "bytes": 5988, "packets": 83, "errors": 0 },    "tx": { "bytes": 9496, "packets": 120, "errors": 0 }  },  "wan": {  "status": [],   "hwaddr": "00:00:00:00:00:00",  "ip": "",   "rx": { "bytes": 0, "packets": 0, "errors": 0 },    "tx": { "bytes": 0, "packets"'... (length=1779)

I need to extract info from WAN like ip or rx...

I have tried using $array['page']['wan']['rx'] but nothing!!!

Upvotes: 0

Views: 71

Answers (1)

sjagr
sjagr

Reputation: 16502

The page value of your $array is not actually an array, but a string. Since it's a JSON string, you should use json_decode to decode the string into a meaningful format for PHP to handle:

$page = json_decode($array['page']);
$pageArray = json_decode($array['page']); // Returned as an array

And to get the RX for example:

var_dump($page->wan->rx); // Returned as an object
var_dump($pageArray['wan']['rx']); // Return as an array

Upvotes: 3

Related Questions