How to make the correct json output?

0 like 0 dislike
7 views
Using foreach iterate and make the data in the array:

foreach ($banners as $key => $id) { $banner = Banner::where('id', $id)->first(); $this->data[$key] = array( 'id' => $banner->id, 'url' => $banner->url, 'name' => $banner->name, 'image' => $banner->image ); }


when json_encode($this->data) receive:

spoiler
5bfce39e994eb274470287.png
by | 7 views

2 Answers

0 like 0 dislike
Try json_encode($this->data, JSON_FORCE_OBJECT);
by
0 like 0 dislike
You here it is:
array( 'id' => $banner->id, 'url' => $banner->url, 'name' => $banner->name, 'image' => $banner->image );

Invest in $this->data key $key, it follows that $this->data is an array, so it decoded in brackets [].
by

Related questions

0 like 0 dislike
2 answers
0 like 0 dislike
1 answer
asked May 21, 2019 by frilanser
0 like 0 dislike
4 answers
0 like 0 dislike
1 answer
110,608 questions
257,186 answers
0 comments
28,740 users