最近做一个足球赛事的功能,查询赛事需要更具 洲 - 国 - 赛事几级联动查询所以前端给的数据格式是:
[{ | |
value: '3', #洲id | |
label: '亚洲联赛', #洲名称 | |
children: [{ | |
value: '45', #国id | |
label: '中国', #国昵称 | |
children: [{ | |
value: '53438', #赛事id | |
label: '中女超杯' #赛事昵称 | |
}, { | |
value: '534389', | |
label: '中女超' | |
}] | |
}, { | |
value: '47', | |
label: '南韩', | |
children: [ | |
{ | |
value: '53333', | |
label: '韩K2联' | |
}, { | |
value: '54085', | |
label: '韩K4联' | |
}] | |
}], | |
value: '1', #洲id | |
label: '欧洲联赛', #洲名称 | |
children: [{ | |
value: '45', #国id | |
label: '中国', #国昵称 | |
children: [{ | |
value: '53438', #赛事id | |
label: '中女超杯' #赛事昵称 | |
}, { | |
value: '534389', | |
label: '中女超' | |
}] | |
}, { | |
value: '47', | |
label: '南韩', | |
children: [ | |
{ | |
value: '53333', | |
label: '韩K2联' | |
}, { | |
value: '54085', | |
label: '韩K4联' | |
}] | |
}] | |
}] |
最后大费周折还请教别人(之前搞过很久没搞忘记了) 才搞定,上实现代码:
$result = [ | |
['value' =>0,'label' =>'国际联赛'], | |
['value' =>1,'label' =>'欧洲联赛'], | |
['value' =>2,'label' =>'美洲联赛'], | |
['value' =>3,'label' =>'亚洲联赛'], | |
['value' =>4,'label' =>'大洋洲联赛'], | |
['value' =>5,'label' =>'非洲联赛'], | |
]; | |
foreach ($result as $k => &$v) { | |
$v['children'] = array_map('get_object_vars',DB::select("select countryId as value,countryCn as label from `league` where (`areaId` = {$v['value']}) group by `countryCn`")); | |
} | |
foreach ($result as $k => &$v) { | |
foreach ($v['children'] as $k1 => &$v1) { | |
$v1['children'] = array_map('get_object_vars',DB::select("select id as value,nameChsShort as label from `league` where (`areaId` = {$v['value']}) and (`countryId` = {$v1['value']})")); | |
} | |
} |