我有table
我想检索一个json字符串的格式
[{
"topic_id":"1",
"topic_title":"Input and Output devices",
"topic_image":"inputs.png",
"subtopics": {
"subtopic_id":"1",
"subtopic_title":"Inputs",
"subtopic_file_url":"0"
},
{
"subtopic_id":"2",
"subtopic_title":"Outputs",
"subtopic_file_url":"0"
}
}]
我试过的
// set the resulting array to associative
$result = $stmt->fetchAll(\PDO::FETCH_ASSOC);
foreach ($result as $key => $value) {
$topic[$key]['topic_id'] = $value['topic_id'];
$topic[$key]['topic_title'] = $value['topic_title'];
$topic[$key]['topic_image'] = $value['topic_image'];
$topic[$key]['subtopics']['subtopic_id'] = $value['subtopicid'];
$topic[$key]['subtopics']['subtopic_title'] = $value['subtopic_title'];
$topic[$key]['subtopics']['subtopic_file_url'] = $value['file_url'];
$topic[$key]['subtopics']['subtopicid'] = $value['subtopicid'];
}
print 'PHP_ARRAY: ';print_r($topic);
$topic = json_encode ( $topic );
上面的脚本返回每个子主题的主题详细信息。
1条答案
按热度按时间kjthegm61#