0

I'm trying to get the following json object:

{
 "Customers":[
  {
   "name": "Jason",
   "country": "USA"
  }
 ]
}

This is my current code :

$data = [
    'Customers' => [
      'name' => "Jason",
      'country' => "USA"
  ]
];

but it doesn't give me the good result:

{"Customers": {"name": "Jason", "country": "USA"}}

Any idea please ?

1 Answer 1

3

Customers contains an array which you were missing

$data = [
    'Customers' => [
        ['name' => "Jason", 'country' => "USA"]
  ]
];

print_r(json_encode($data));

RESULT

{"Customers":[{"name":"Jason","country":"USA"}]}

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.