Support the ongoing development of →
Session Database Eloquent
Last updated 2 years ago.

The keys aren't lost. Keys will still start at 0 for the json output


As fourth parameter json method accepts json_encode options. You can use JSON_FORCE_OBJECT to encode array as object.

echo response()->json(['one', 'two'], 200, [], , JSON_FORCE_OBJECT );
 // {"0":"one","1":"two"}
Last updated 7 years ago.

organizm238, semalley liked this reply



Why would you force the JSON method to encode an array as an object? Unless you want to pass an associative array to javascript, keep your variable types consistent.


Sign in to participate in this thread!


Your banner here too?

AtomicRSA atomicrsa Joined 24 Jun 2015


We'd like to thank these amazing companies for supporting us

Your logo here?

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 - All rights reserved.