Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize an empty associative array in PHP?

Tags:

php

symfony

I want to have an empty object in the API response like the one mentioned below:

{
  "sample" : {}
}

When I assign $response['sample'] = array(), I get the following response

{
  "sample" : []
}
like image 519
Saish Sali Avatar asked Mar 15 '26 22:03

Saish Sali


1 Answers

{} indicates an object so you have to use

$result = ["sample" => new stdclass()];
like image 158
lumos0815 Avatar answered Mar 17 '26 12:03

lumos0815



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!