1 回答
TA贡献1848条经验 获得超10个赞
这里的代码描述了逻辑应该如何在您的结果集上运行以获得所需的格式,其中一些虚拟数组数据与您的查询结果匹配。
$test = array( array( "collection_id" => 33, 'heading' => 'test3', 'category_id' => 3, 'text' => 'dfsfdsfd1' ),
array( "collection_id" => 33, 'heading' => 'test3', 'category_id' => 4, 'text' => 'dfsfdsfd2' ),
array( "collection_id" => 34, 'heading' => 'test4', 'category_id' => 5, 'text' => 'dfsfdsfd3' ),
array( "collection_id" => 34, 'heading' => 'test4', 'category_id' => 6, 'text' => 'dfsfdsfd4' )
);
$res= array();
foreach ($test as $key => $value) {
if( !isset($res[$value['collection_id']]) ){
$res[$value['collection_id']] = array();
$res[$value['collection_id']]['collection_id'] = $value['collection_id'];
$res[$value['collection_id']]['heading'] = $value['heading'];
}
$colarr = isset($res[$value['collection_id']]['category']) && is_array($res[$value['collection_id']]['category']) ? $res[$value['collection_id']]['category'] : array();
$colarr[] = array( 'category_id' => $value['category_id'], 'text' => $value['text'] );
$res[$value['collection_id']]['category'] = $colarr;
}
$output = array();
$output['collection-category'] = array_values($res);
echo json_encode($output);
你可以在你的代码中试试这个。
- 1 回答
- 0 关注
- 108 浏览
添加回答
举报