1 回答

TA貢獻(xiàn)1848條經(jīng)驗(yàn) 獲得超10個贊
這里的代碼描述了邏輯應(yīng)該如何在您的結(jié)果集上運(yùn)行以獲得所需的格式,其中一些虛擬數(shù)組數(shù)據(jù)與您的查詢結(jié)果匹配。
$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 關(guān)注
- 133 瀏覽
添加回答
舉報