Florian Sauerwein Florian Sauerwein - 2 months ago 10
JSON Question

Create JSON object with array array as attribute in PHP

I'm trying to create a JSON-String that looks like this:

{"id":"1","name":"new group test","beschreibung":"this is a description","gewerbe":"1" , "members":[{"uniqueid":"100110001"},{"uniqueid":"100110002"},{"uniqueid":"100110003"}]}


Right now my output looks like this:

{"data":{"id":"1","name":"new group test","beschreibung":"this is a ne","gewerbe":"1"},"members":[{"uniqueid":"100110001"},{"uniqueid":"100110002"},{"uniqueid":"100110003"}]}


Using the following PHP code:

return array('data' => $data, 'members' => $members);


$data is the result of a SQL-query and $members is a result array of another SQL-query. I want to have my members array as an attribute inside of data like in the example I posted. How do I create string like that?

Answer
$data['members'] = $members;
return json_encode($data);
Comments