Support the ongoing development of Laravel.io →
Session Database Eloquent

Hi

dd my array looks like this:

array:8 [▼
  0 => "promo\1\images\2.jpg"
  1 => "promo\1\images\20150927_100644.jpg"
  2 => "promo\1\main\1.jpg"
  3 => "promo\3\images\2.jpg"
  4 => "promo\3\images\20150927_100644.jpg"
  5 => "promo\3\main\1.jpg"
  6 => "promo\4\images\2.jpg"
  7 => "promo\4\main\1.jpg"
]

When i do this

return response()->json($path);

The key is lost and the output looks like this:

["promo\\1\\images\\2.jpg","promo\\1\\images\\20150927_100644.jpg","promo\\1\\main\\1.jpg","promo\\3\\images\\2.jpg","promo\\3\\images\\20150927_100644.jpg","promo\\3\\main\\1.jpg","promo\\4\\images\\2.jpg","promo\\4\\main\\1.jpg"]

I cannot find anything to solve this.

Thanks

Last updated 3 years ago.
0

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

0

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 8 years ago.

organizm238, semalley liked this reply

2

@Ognjen

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.

0

Sign in to participate in this thread!

PHPverse

Your banner here too?

AtomicRSA atomicrsa Joined 24 Jun 2015

Moderators

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

Your logo here?

Laravel.io

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

© 2025 Laravel.io - All rights reserved.