Taylorbyte.com

IT solutions, linux hosting, web design and computer services

Author Topic: read JSON into Multidimensional Array PHP  (Read 21498 times)

brenton

  • Administrator
  • Newbie
  • *****
  • Posts: 5
    • View Profile
    • Taylorbyte.com
read JSON into Multidimensional Array PHP
« on: January 15, 2013, 04:04:59 pm »
How can i read a multidimensional JSON array into a multidimensional PHP array

JSON:
Code: [Select]
{
  'updates': {
    'ssid':'tb-hotspot1',
    'mac':'00:15:6D:1A:A2:E2',
    'clientusage': [
      {
        'ip':'192.168.182.232',
        'used':'500000'
      },
      {
        'ip':'192.168.182.200',
        'used':'500000'
      }
    ]
  }
}


Sent POST to PHP server with curl in bash:
Code: [Select]
curl -k -H "Accept: application/json" -H "Content-Type: application/json" -X POST -d $json https://myserver.xx/uam.php

Receive POST from curl into PHP:
Code: [Select]
<?php
$json 
file_get_contents("php://input");
echo 
"\n";
print_r($json);
echo 
"\n";
echo 
"\n";
var_dump(json_decode($json));
echo 
"\n";
echo 
"\n";
$obj=json_decode($json);
print 
$obj->Updates[0]->{'SSID'};  //help needed with this line
?>

Arch-linux, NVIDIA multiseat, SSD, btrfs, GPT.
Linux as main OS since 2007

brenton

  • Administrator
  • Newbie
  • *****
  • Posts: 5
    • View Profile
    • Taylorbyte.com
Re: read JSON into Multidimensional Array PHP
« Reply #1 on: January 15, 2013, 04:19:16 pm »
The ",true" makes it into an associative array... Didn't read the php.net manual properly  :-[

Code: [Select]
<?php
////////////////////

$json_array=json_decode($json,true);
echo 
"SSID: ".$json_array['Updates']['SSID']."\n";
echo 
"MAC: ".$json_array['Updates']['MAC']."\n";
?>
Arch-linux, NVIDIA multiseat, SSD, btrfs, GPT.
Linux as main OS since 2007