I am trying to accomplish what TJ Crowder suggested in JSON find in JavaScript but can't seem to convert an array into an object and get the same results he suggests in his first answer.
Whenever I convert the array into an object and print out both the 'object' and the array they look identical.
My controller:
public function baseball(){
$users = \DB::table('users')->get();
return view('tools.baseball')->with('users', json_encode($users, true));
}
FYI: I have tried to convert 'users' into an object 'players' in the controller, then pass the players object to the view by using $players = json_decode(json_encode($users)); - but this seems to pass through a null object (nothing prints when I print_r the object)
My view:
<?php
$players = (json_decode(json_encode($users), FALSE));
print_r($users);
echo "<hr>";
print_r($players);
?>
My result:
[ { "id":"1","name":"Player Player","avg":"400","gp":"2","gs":"2","ab":"5","r":"0","h":"2","2b":"1","3b":"0","hr":"0","rbi":"0","bb":"0","k":"1","sb":"0","obp":"400","slg":"600","created_at":null,"updated_at":null }]
[{"id":"1","name":"Player Player","avg":"400","gp":"2","gs":"2","ab":"5","r":"0","h":"2","2b":"1","3b":"0","hr":"0","rbi":"0","bb":"0","k":"1","sb":"0","obp":"400","slg":"600","created_at":null,"updated_at":null}]
In TJ's example he accomplishes
{
"one": {"pId": "foo1", "cId": "bar1"},
"two": {"pId": "foo2", "cId": "bar2"},
"three": {"pId": "foo3", "cId": "bar3"}
}
But I obviously am not getting this result with my output. What am I doing wrong?
I'd like to accomplish
{
"Player Player": {"avg": "400", "gp": "2"},
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire