$arr=[
0 =>[
"id" =>"2",
"cate_name" =>"外用",
"parent_id" => "0",
"child" => [
0 => [
"id" => "5",
"cate_name" => "外1",
"parent_id" =>"2",
"child" =>[
0 => [
"id" =>"7",
"cate_name" => "外外1",
"parent_id" => "5",
"child" => NULL
]
]
],
1=>[
"id" => "6",
"cate_name" => "外2",
"parent_id" => "2",
"child" => NULL,
]
]
],
1 => [
"id" => "3",
"cate_name" =>"内用",
"parent_id" => "0",
"child" => NULL,
]
]
将这个三维数组分类树,组合成这种一维数组
$arr=[
0=>["id" =>"2","cate_name" =>"外用","parent_id" => "0",], //顶级分类
1=>["id" => "5","cate_name" => "外1","parent_id" =>"2",], //二级分类
2=>["id" =>"7","cate_name" => "外外1","parent_id" => "5",], //三级分类
3=>["id" => "6","cate_name" => "外2","parent_id" => "2",], //二级分类
4=>["id" => "3","cate_name" =>"内用","parent_id" => "0",],//顶级分类
];
求大神们帮忙。
2 回答
紫衣仙女
TA贡献1839条经验 获得超15个赞
这个没难度啊
$i=0;
foreach ($arr as $key => $value) {
if(is_array($value['child'])){
foreach ($value['child'] as $k => $v) {
$array[$i]['id']=$v['id'];
$array[$i]['cate_name']=$v['cate_name'];
$array[$i]['parent_id']=$v['parent_id'];
$i++;
}
}
}
var_dump($array);
斯蒂芬大帝
TA贡献1827条经验 获得超8个赞
$result = [];
function getChild($parentId, $child)
{
global $result;
foreach ($child as $item) {
$result[] = [
'id' =>$item['id'],
'cate_name' => $item['cate_name'],
'parent_id' => $parentId
];
if ($item['child']) {
getChild($item['id'], $item['child']);
}
}
}
getChild(0, $arr);
var_dump($result);
- 2 回答
- 0 关注
- 412 浏览
添加回答
举报
0/150
提交
取消