简体   繁体   中英

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?

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

Change structure of array:

Instead of

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

use:

$data['members'] = $members;
json_encode($data);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM