2 回答
TA贡献1934条经验 获得超2个赞
最后,我解决了这个
$parents = Category::where('parent_id', 0)->where('status', 1)->orderBy('sort_order', 'asc')->get();
foreach ($parents as $parent) {
$childs = Category::where('parent_id', $parent->id)->where('status', 1)->orderBy('sort_order', 'asc')->get();
if (count($childs) > 0) {
$subCat = array();
$players = array();
$roster[$parent->name] = $players;
foreach ($childs as $i => $child) {
$subchilds = Category::where('parent_id', $child->id)->where('status', 1)->orderBy('sort_order', 'asc')->get();
if (count($subchilds) > 0) {
$roster[$parent->name][$child->name] = $subCat;
foreach ($subchilds as $subchild) {
$roster[$parent->name][$child->name][$subchild->id] = $subchild->name;
}
}else{
$roster[$parent->name][$child->name] = $players;
}
}
}
}
return $roster;
TA贡献1846条经验 获得超7个赞
这是你如何做到的。
$allData = array();
// get all parent category
$categories = Category::where(['status' => 1, 'parent_id' => 0])->get();
foreach ($categories as $key=>$sub) {
// now take one by one it's child category
$allData[$key]['parent'] = $sub->name;
$subCategory = Category::where('status', 1)->where('parent_id', '=', $sub->id)->get();
$subCat = array();
// set parent category title
foreach ($subCategory as $k=>$subcat) {
$subCat[$subcat->id] = $subcat->name;
//children of subcat
$children = Category::where('status', 1)->where('parent_id', '=', $subcat->id)->get();
$child = array();
if($children){
foreach($children as $k1=>$val){
$child[$val->id] = $val->name;
$allData[$key]['subcategory'][$subcat->id]['child'] = $child;
}
}
}
// set subcategory array
$allData[$key]['subcategory'] = $subCat;
}
return $allData;
请尝试这种方式。将您的 json 转换为数组并使用它来获取子值
$ar = json_decode($data,true);
foreach($ar as $value){
echo $value['parent'].'<br>';
foreach($value['child'] as $child){
echo ' '.$child.'<br>';
}
}
这将输出这个
test
A
B
C
D
E
test1
G
H
I
J
K
- 2 回答
- 0 关注
- 89 浏览
添加回答
举报