I can't get my json_encode format that I need.
Current Format:
{
"substantiv": [
{"text":"auto"},
{"text":"noch ein auto"}
],
"verben":[
{"text":"auto fahren"}
]
}
What I need:
[
{
"type":"substantiv",
"items": [
{"text":"auto"},
{"text":"noch ein auto"}
]
} , {
"type":"verben",
"items": [
{"text":"auto fahren"}
]
}
]
My current php code:
$data = array();
while($row = $rs->fetch_assoc()){
$tmp = array(
"text" => $row['gtext']
);
$data[$row['type']][] = $tmp;
}
echo json_encode($data);
I have tried a few things but just can't figure it out.
You could try something like this
while($row = $rs->fetch_assoc()){
$data[$row['type']][] = array(
"text" => $row['gtext']
);
}
$result = array();
foreach($data AS $type => $items) {
$result[] = array(
'type' => $type,
'items' => $items
);
}
echo json_encode($result);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments