It might sound like a bit of a weird question, but i've always struggled to understand the different structures of arrays and objects when passing them via JSON.
How would I build an array in PHP, and pass it back via JSON so that when i console log it out, it displays as a list of array objects like this?
At the moment this is my code
$users = [];
foreach($users as $key => $user){
$array = [];
$array['username'] = $user->username;
$array['id'] = $user->id;
array_push($users, $array);
}
return response()->json(['list'=> $users]);
But this returns it so it looks like this
{list: [{username: 'Joe Bloggs', id: 1}, {username: 'John Smith', id 2}]}
in the console log.
How would i structure my php so that when I pass it back via JSON, and console log it out in the browser, it looks like the picture above? With a dropdown arrow, and an array of objects for me to click through?
The different types of arrays and objects always confuses me, and I kind of just do trial and error until it works the way I want, but I want to understand how it works.
I just cant seem to wrap my head around it!
EDIT
Just to add some extra information to try and get across what I am trying to do.
At the moment my javascript is console.log('Data Returned', result);
.
In my PHP, if I grab a class in Laravel using
return response()->json(['list'=> User::all()]);
When I pass it back, it displays like the picture I have attached, with a dropdown and an array of objects.
If I then change the PHP to try and build the object myself before passing it back, and I change it to the above PHP code, it displays like this in the console log {list: [{username: 'Joe Bloggs', id: 1}, {username: 'John Smith', id 2}]}
without me changing the javascript.
How do I manually build an object in PHP, so that when I pass it back, it console logs out like the picture above, with an array of objects and a dropdown to click through them all?