1

I have this code:

$arr = [];

$arr['into']['box'] = 1;

return $arr;

But it returns this:

{
  "into": {
    "box": 1
  }
}

As you can see it should be array, not object!!

I searched a lot but I didn't find anything.

Help me and give me the code or the tip please.

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
Gamble guy
  • 11
  • 2
  • your `$arr` is multidimentional array, it uses named keys. That's why. What output do you expect? – bertdida Aug 09 '20 at 02:05
  • This should help https://stackoverflow.com/questions/51087064/laravel-grouped-collection-returns-object-instead-of-array – my_workbench Aug 09 '20 at 02:57

1 Answers1

0

From the docs:

In addition to returning strings from your routes and controllers, you may also return arrays. The framework will automatically convert the array into a JSON response:

The same is true for models, and collections:

Did you know you can also return Eloquent collections from your routes or controllers? They will automatically be converted to JSON. Give it a shot!

Don't Panic
  • 13,965
  • 5
  • 32
  • 51
  • Thanks @dont-panic for your reply. I cant do it also in pure PHP, is there any way?! – Gamble guy Aug 09 '20 at 00:02
  • @Gambleguy [Here's an example](https://stackoverflow.com/questions/4064444/returning-json-from-a-php-script) of manually doing what Laravel is doing automatically behind the scenes, if that's what you mean. – Don't Panic Aug 09 '20 at 22:52
  • @Gambleguy Also, https://stackoverflow.com/help/someone-answers :-) – Don't Panic Aug 09 '20 at 22:53