I want to format json
in this format for js
library! GoJs expect json in this format:
model.nodeDataArray =
[
{ key: "1", username: "Don Meow", source: "cat1.png" },
{ key: "2", parent: "1", username: "Demeter", source: "cat2.png" },
{ key: "3", parent: "1", username: "Copricat", source: "cat3.png" },
{ key: "4", parent: "3", username: "Jellylorum", source: "cat4.png" },
{ key: "5", parent: "3", username: "Alonzo", source: "cat5.png" },
{ key: "6", parent: "2", username: "Munkustrap", source: "cat6.png" }
];
In php i try to return json like above but am realy new to json and my example dont work!
$users = $db->query("SELECT * FROM user");
$data = array();
while ($result = $users->fetch_assoc())
{
$data['key'] = $result['id'];
$data['username'] = $result['username'];
$data['email'] = $result['email'];
$data['parent'] = $result['parent'];
array_push($data, $result);
}
echo json_encode($data);
My JSON looks like this:
{"key":"7","username":"Vlada","parent":"4","0":{"id":"1","parent":null,"username":"Ivan","email":"office.asd@gmail.com","password":"qwe123"},"1":{"id":"2","parent":"1","username":"Martinu","email":"asd@gmail.com","password":"qwe123"},"2":{"id":"3","parent":"1","username":"Biljana","email":"asd.com","password":"qwe123"},"3":{"id":"4","parent":"2","username":"Emil","email":"test@test.com","password":null},"4":{"id":"5","parent":"2","username":"Elena","email":"test@test.com","password":null},"5":{"id":"6","parent":"4","username":"Bole","email":null,"password":null},"6":{"id":"7","parent":"4","username":"Vlada","email":null,"password":null}}
I try to replace id
with key
becouse GoJs need key
property defined. My json is so so different and i need to format output like above json?
What i do wrong here ?