在写一个商品分类的接口,之前是php混合开发就直接把option echo 出去了,达到了效果,现在是要把所有的option组合成字符串,返回出去,中间经历了几次坑。
原代码(混合开发的):
function category_option( $list , $grade = 1 )
{
foreach ($list as $v)
{
echo "<option value='".$v['cat_id']."'>".space($grade).$v['cat_name']."</option>";
if(!empty($v['child'])){
category_option( $v['child'] , $v['grade']+1);
}
}
}
接口代码:
function category_option2( $list , $grade = 1)
{
$str = '';
foreach ($list as $v)
{
$str .= "<option value='".$v['cat_id']."'>".space($grade).$v['cat_name']."</option>";
if(!empty($v['child']))}
$str .= category_option2( $v['child'] , $v['grade']+1);
}
}
return $str;
}
这个是对数据事先处理的,数据结构如下:
array(7) { [0] => array(4) { ["cat_id"] => int(4) ["cat_name"] => string(12) "休闲娱乐" ["parent_id"] => int(0) ["child"] => array(3) { [0] => array(4) { ["cat_id"] => int(18) ["cat_name"] => string(6) "互动" ["parent_id"] => int(4) ["child"] => array(2) { [0] => array(4) { ["cat_id"] => int(19) ["cat_name"] => string(6) "视频" ["parent_id"] => int(18) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(26) ["cat_name"] => string(9) "小游戏" ["parent_id"] => int(18) ["child"] => array(0) { } } } } [1] => array(4) { ["cat_id"] => int(72) ["cat_name"] => string(6) "直播" ["parent_id"] => int(4) ["child"] => array(0) { } } [2] => array(4) { ["cat_id"] => int(210) ["cat_name"] => string(9) "日用品" ["parent_id"] => int(4) ["child"] => array(0) { } } } } [1] => array(4) { ["cat_id"] => int(32) ["cat_name"] => string(6) "美食" ["parent_id"] => int(0) ["child"] => array(5) { [0] => array(4) { ["cat_id"] => int(70) ["cat_name"] => string(12) "订餐送餐" ["parent_id"] => int(32) ["child"] => array(3) { [0] => array(4) { ["cat_id"] => int(215) ["cat_name"] => string(6) "早餐" ["parent_id"] => int(70) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(216) ["cat_name"] => string(6) "中餐" ["parent_id"] => int(70) ["child"] => array(0) { } } [2] => array(4) { ["cat_id"] => int(217) ["cat_name"] => string(6) "晚餐" ["parent_id"] => int(70) ["child"] => array(0) { } } } } [1] => array(4) { ["cat_id"] => int(211) ["cat_name"] => string(12) "宴请聚餐" ["parent_id"] => int(32) ["child"] => array(7) { [0] => array(4) { ["cat_id"] => int(218) ["cat_name"] => string(6) "聚餐" ["parent_id"] => int(211) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(219) ["cat_name"] => string(6) "火锅" ["parent_id"] => int(211) ["child"] => array(0) { } } [2] => array(4) { ["cat_id"] => int(220) ["cat_name"] => string(6) "海鲜" ["parent_id"] => int(211) ["child"] => array(0) { } } [3] => array(4) { ["cat_id"] => int(245) ["cat_name"] => string(6) "西餐" ["parent_id"] => int(211) ["child"] => array(0) { } } [4] => array(4) { ["cat_id"] => int(246) ["cat_name"] => string(6) "烤串" ["parent_id"] => int(211) ["child"] => array(0) { } } [5] => array(4) { ["cat_id"] => int(247) ["cat_name"] => string(9) "自助餐" ["parent_id"] => int(211) ["child"] => array(0) { } } [6] => array(4) { ["cat_id"] => int(248) ["cat_name"] => string(9) "生日餐" ["parent_id"] => int(211) ["child"] => array(0) { } } } } [2] => array(4) { ["cat_id"] => int(212) ["cat_name"] => string(12) "大厨食材" ["parent_id"] => int(32) ["child"] => array(5) { [0] => array(4) { ["cat_id"] => int(224) ["cat_name"] => string(6) "蔬菜" ["parent_id"] => int(212) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(225) ["cat_name"] => string(6) "水果" ["parent_id"] => int(212) ["child"] => array(0) { } } [2] => array(4) { ["cat_id"] => int(226) ["cat_name"] => string(12) "净菜食材" ["parent_id"] => int(212) ["child"] => array(0) { } } [3] => array(4) { ["cat_id"] => int(249) ["cat_name"] => string(12) "早中午餐" ["parent_id"] => int(212) ["child"] => array(0) { } } [4] => array(4) { ["cat_id"] => int(250) ["cat_name"] => string(12) "干菜佐料" ["parent_id"] => int(212) ["child"] => array(0) { } } } } [3] => array(4) { ["cat_id"] => int(213) ["cat_name"] => string(12) "健康食品" ["parent_id"] => int(32) ["child"] => array(1) { [0] => array(4) { ["cat_id"] => int(223) ["cat_name"] => string(12) "包装熟食" ["parent_id"] => int(213) ["child"] => array(0) { } } } } [4] => array(4) { ["cat_id"] => int(214) ["cat_name"] => string(12) "健康饮水" ["parent_id"] => int(32) ["child"] => array(2) { [0] => array(4) { ["cat_id"] => int(221) ["cat_name"] => string(6) "水杯" ["parent_id"] => int(214) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(222) ["cat_name"] => string(9) "过滤器" ["parent_id"] => int(214) ["child"] => array(0) { } } } } } } [2] => array(4) { ["cat_id"] => int(35) ["cat_name"] => string(12) "学习辅导" ["parent_id"] => int(0) ["child"] => array(5) { [0] => array(4) { ["cat_id"] => int(227) ["cat_name"] => string(12) "课程辅导" ["parent_id"] => int(35) ["child"] => array(6) { [0] => array(4) { ["cat_id"] => int(228) ["cat_name"] => string(6) "早教" ["parent_id"] => int(227) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(229) ["cat_name"] => string(6) "幼教" ["parent_id"] => int(227) ["child"] => array(0) { } } [2] => array(4) { ["cat_id"] => int(230) ["cat_name"] => string(6) "小教" ["parent_id"] => int(227) ["child"] => array(0) { } } [3] => array(4) { ["cat_id"] => int(231) ["cat_name"] => string(6) "中教" ["parent_id"] => int(227) ["child"] => array(0) { } } [4] => array(4) { ["cat_id"] => int(232) ["cat_name"] => string(6) "高教" ["parent_id"] => int(227) ["child"] => array(0) { } } [5] => array(4) { ["cat_id"] => int(233) ["cat_name"] => string(6) "职教" ["parent_id"] => int(227) ["child"] => array(0) { } } } } [1] => array(4) { ["cat_id"] => int(234) ["cat_name"] => string(12) "考试题库" ["parent_id"] => int(35) ["child"] => array(2) { [0] => array(4) { ["cat_id"] => int(235) ["cat_name"] => string(6) "月考" ["parent_id"] => int(234) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(236) ["cat_name"] => string(12) "错题点评" ["parent_id"] => int(234) ["child"] => array(0) { } } } } [2] => array(4) { ["cat_id"] => int(237) ["cat_name"] => string(9) "辅导班" ["parent_id"] => int(35) ["child"] => array(0) { } } [3] => array(4) { ["cat_id"] => int(238) ["cat_name"] => string(12) "特长测试" ["parent_id"] => int(35) ["child"] => array(0) { } } [4] => array(4) { ["cat_id"] => int(256) ["cat_name"] => string(12) "推荐书籍" ["parent_id"] => int(35) ["child"] => array(4) { [0] => array(4) { ["cat_id"] => int(257) ["cat_name"] => string(6) "童书" ["parent_id"] => int(256) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(258) ["cat_name"] => string(6) "文艺" ["parent_id"] => int(256) ["child"] => array(0) { } } [2] => array(4) { ["cat_id"] => int(259) ["cat_name"] => string(12) "青春励志" ["parent_id"] => int(256) ["child"] => array(0) { } } [3] => array(4) { ["cat_id"] => int(260) ["cat_name"] => string(6) "社科" ["parent_id"] => int(256) ["child"] => array(0) { } } } } } } [3] => array(4) { ["cat_id"] => int(104) ["cat_name"] => string(6) "健康" ["parent_id"] => int(0) ["child"] => array(6) { [0] => array(4) { ["cat_id"] => int(142) ["cat_name"] => string(12) "验光配镜" ["parent_id"] => int(104) ["child"] => array(19) { [0] => array(4) { ["cat_id"] => int(113) ["cat_name"] => string(12) "超轻镜架" ["parent_id"] => int(142) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(114) ["cat_name"] => string(12) "金属镜架" ["parent_id"] => int(142) ["child"] => array(0) { } } [2] => array(4) { ["cat_id"] => int(115) ["cat_name"] => string(12) "儿童镜架" ["parent_id"] => int(142) ["child"] => array(0) { } } [3] => array(4) { ["cat_id"] => int(116) ["cat_name"] => string(15) "青少年镜架" ["parent_id"] => int(142) ["child"] => array(0) { } } [4] => array(4) { ["cat_id"] => int(153) ["cat_name"] => string(12) "品牌镜片" ["parent_id"] => int(142) ["child"] => array(0) { } } [5] => array(4) { ["cat_id"] => int(154) ["cat_name"] => string(12) "光学镜片" ["parent_id"] => int(142) ["child"] => array(0) { } } [6] => array(4) { ["cat_id"] => int(155) ["cat_name"] => string(12) "功能镜片" ["parent_id"] => int(142) ["child"] => array(0) { } } [7] => array(4) { ["cat_id"] => int(156) ["cat_name"] => string(12) "特殊镜片" ["parent_id"] => int(142) ["child"] => array(0) { } } [8] => array(4) { ["cat_id"] => int(157) ["cat_name"] => string(9) "验光师" ["parent_id"] => int(142) ["child"] => array(0) { } } [9] => array(4) { ["cat_id"] => int(188) ["cat_name"] => string(12) "商务镜架" ["parent_id"] => int(142) ["child"] => array(0) { } } [10] => array(4) { ["cat_id"] => int(206) ["cat_name"] => string(9) "定制片" ["parent_id"] => int(142) ["child"] => array(0) { } } [11] => array(4) { ["cat_id"] => int(208) ["cat_name"] => string(12) "隐形眼镜" ["parent_id"] => int(142) ["child"] => array(0) { } } [12] => array(4) { ["cat_id"] => int(253) ["cat_name"] => string(12) "单光镜片" ["parent_id"] => int(142) ["child"] => array(0) { } } [13] => array(4) { ["cat_id"] => int(254) ["cat_name"] => string(12) "高折镜片" ["parent_id"] => int(142) ["child"] => array(0) { } } [14] => array(4) { ["cat_id"] => int(261) ["cat_name"] => string(12) "隐形月抛" ["parent_id"] => int(142) ["child"] => array(0) { } } [15] => array(4) { ["cat_id"] => int(262) ["cat_name"] => string(12) "隐形季抛" ["parent_id"] => int(142) ["child"] => array(0) { } } [16] => array(4) { ["cat_id"] => int(263) ["cat_name"] => string(15) "隐形半年抛" ["parent_id"] => int(142) ["child"] => array(0) { } } [17] => array(4) { ["cat_id"] => int(264) ["cat_name"] => string(12) "隐形年抛" ["parent_id"] => int(142) ["child"] => array(0) { } } [18] => array(4) { ["cat_id"] => int(265) ["cat_name"] => string(12) "隐形日抛" ["parent_id"] => int(142) ["child"] => array(0) { } } } } [1] => array(4) { ["cat_id"] => int(144) ["cat_name"] => string(12) "增智健脑" ["parent_id"] => int(104) ["child"] => array(5) { [0] => array(4) { ["cat_id"] => int(183) ["cat_name"] => string(12) "维生素饮" ["parent_id"] => int(144) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(184) ["cat_name"] => string(12) "微量元素" ["parent_id"] => int(144) ["child"] => array(0) { } } [2] => array(4) { ["cat_id"] => int(186) ["cat_name"] => string(12) "健康食品" ["parent_id"] => int(144) ["child"] => array(0) { } } [3] => array(4) { ["cat_id"] => int(195) ["cat_name"] => string(12) "健脑食品" ["parent_id"] => int(144) ["child"] => array(0) { } } [4] => array(4) { ["cat_id"] => int(196) ["cat_name"] => string(12) "健脑用品" ["parent_id"] => int(144) ["child"] => array(0) { } } } } [2] => array(4) { ["cat_id"] => int(151) ["cat_name"] => string(12) "眼睛防护" ["parent_id"] => int(104) ["child"] => array(5) { [0] => array(4) { ["cat_id"] => int(131) ["cat_name"] => string(9) "老花镜" ["parent_id"] => int(151) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(158) ["cat_name"] => string(9) "太阳镜" ["parent_id"] => int(151) ["child"] => array(0) { } } [2] => array(4) { ["cat_id"] => int(160) ["cat_name"] => string(12) "视力训练" ["parent_id"] => int(151) ["child"] => array(0) { } } [3] => array(4) { ["cat_id"] => int(161) ["cat_name"] => string(12) "护眼方案" ["parent_id"] => int(151) ["child"] => array(0) { } } [4] => array(4) { ["cat_id"] => int(203) ["cat_name"] => string(12) "视觉健康" ["parent_id"] => int(151) ["child"] => array(0) { } } } } [3] => array(4) { ["cat_id"] => int(189) ["cat_name"] => string(12) "面部护理" ["parent_id"] => int(104) ["child"] => array(5) { [0] => array(4) { ["cat_id"] => int(190) ["cat_name"] => string(12) "美鼻矫形" ["parent_id"] => int(189) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(191) ["cat_name"] => string(12) "美牙矫形" ["parent_id"] => int(189) ["child"] => array(0) { } } [2] => array(4) { ["cat_id"] => int(192) ["cat_name"] => string(12) "美眼矫形" ["parent_id"] => int(189) ["child"] => array(0) { } } [3] => array(4) { ["cat_id"] => int(193) ["cat_name"] => string(12) "美脸矫形" ["parent_id"] => int(189) ["child"] => array(0) { } } [4] => array(4) { ["cat_id"] => int(194) ["cat_name"] => string(12) "生发祛痘" ["parent_id"] => int(189) ["child"] => array(0) { } } } } [4] => array(4) { ["cat_id"] => int(197) ["cat_name"] => string(12) "肢体保健" ["parent_id"] => int(104) ["child"] => array(5) { [0] => array(4) { ["cat_id"] => int(198) ["cat_name"] => string(12) "饮水健康" ["parent_id"] => int(197) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(199) ["cat_name"] => string(12) "肢体防护" ["parent_id"] => int(197) ["child"] => array(0) { } } [2] => array(4) { ["cat_id"] => int(200) ["cat_name"] => string(6) "减肥" ["parent_id"] => int(197) ["child"] => array(0) { } } [3] => array(4) { ["cat_id"] => int(201) ["cat_name"] => string(6) "增高" ["parent_id"] => int(197) ["child"] => array(0) { } } [4] => array(4) { ["cat_id"] => int(202) ["cat_name"] => string(12) "腰背防护" ["parent_id"] => int(197) ["child"] => array(0) { } } } } [5] => array(4) { ["cat_id"] => int(205) ["cat_name"] => string(12) "联盟活动" ["parent_id"] => int(104) ["child"] => array(0) { } } } } [4] => array(4) { ["cat_id"] => int(111) ["cat_name"] => string(12) "成长帮扶" ["parent_id"] => int(0) ["child"] => array(0) { } } [5] => array(4) { ["cat_id"] => int(239) ["cat_name"] => string(12) "文化旅游" ["parent_id"] => int(0) ["child"] => array(5) { [0] => array(4) { ["cat_id"] => int(240) ["cat_name"] => string(9) "名人游" ["parent_id"] => int(239) ["child"] => array(0) { } } [1] => array(4) { ["cat_id"] => int(241) ["cat_name"] => string(9) "名地游" ["parent_id"] => int(239) ["child"] => array(0) { } } [2] => array(4) { ["cat_id"] => int(242) ["cat_name"] => string(9) "体验游" ["parent_id"] => int(239) ["child"] => array(0) { } } [3] => array(4) { ["cat_id"] => int(243) ["cat_name"] => string(9) "亲子营" ["parent_id"] => int(239) ["child"] => array(0) { } } [4] => array(4) { ["cat_id"] => int(244) ["cat_name"] => string(12) "营地教育" ["parent_id"] => int(239) ["child"] => array(0) { } } } } [6] => array(4) { ["cat_id"] => int(251) ["cat_name"] => string(6) "其它" ["parent_id"] => int(0) ["child"] => array(1) { [0] => array(4) { ["cat_id"] => int(252) ["cat_name"] => string(12) "积分兑换" ["parent_id"] => int(251) ["child"] => array(0) { } } } } }