-1

How do I read all the users from this POST.

    {
    "refId":"455",
    "users": [
    { 
        "name":"tom",
        "address":"adres1",
        "city":"city1"
    },
    { 
        "name":"JAck",
        "address":"adres2",
        "city":"city3"
    }
]
}

the 'refId' I get like this

$data = json_decode( file_get_contents( "php://input" ) );
$refID = $data->refId;

I there is only one user I read like this

$userName = $data->users->name;
$userAddress = $data->users->address;
$usercity = $data->users->city;

But when there are 2 or more users, I get stuck... probably a 'foreach' but I can't figure it out..

Koen Amant
  • 17
  • 3

1 Answers1

0

Found it... Corrected the JSON structure and added the 'true' on the json_decode(..., true)

$data = json_decode( file_get_contents( "php://input" ), true );
$users = $data[ 'users' ];
  foreach ( $users as $user ) {
    $name = $user[ 'name' ];
    $address = $user[ 'address' ];
    $city = $user[ 'city' ];
    //do something
  }
Koen Amant
  • 17
  • 3