Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在JSON Laravel中获取子类别和子类别用户类别下的子类别_Php_Arrays_Loops_Laravel 5 - Fatal编程技术网

Php 如何在JSON Laravel中获取子类别和子类别用户类别下的子类别

Php 如何在JSON Laravel中获取子类别和子类别用户类别下的子类别,php,arrays,loops,laravel-5,Php,Arrays,Loops,Laravel 5,这是一个非常常见的问题,但我无法将其转换为API的JSON格式。我创建了一个表Category,其中存储了三层Category。类别->子类别->子类别。 我想得到这个API的格式 差不多 Category sub-category-1 sub-category-2 child-2-1 child-2-2 sub-category-3 我尝试使用代码 $allData = array(); // get all parent

这是一个非常常见的问题,但我无法将其转换为API的JSON格式。我创建了一个表Category,其中存储了三层Category。类别->子类别->子类别。 我想得到这个API的格式

差不多

Category

  sub-category-1
  sub-category-2 
      child-2-1
      child-2-2
  sub-category-3
我尝试使用代码

  $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;

                    }
                    // set subcategory array
                    $allData[$key]['subcategory'] = $subCat;
            }
           return $allData;
我得到了这个结果,但我不知道如何获得子值

数据库看起来像


以下是您可以做到的方法

 $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 '&nbsp;'.$child.'<br>';
   }
 }

这是你可以做到的

 $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 '&nbsp;'.$child.'<br>';
   }
 }

最后,我解决了这个问题

       $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;

最后,我解决了这个问题

       $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;

谢谢您的回答,但我还需要子类别的子类别您当前的数组是什么样子的?在问题中,我提到了当前结果的图像。您只添加了父级和子级,父级是类别,子级是子类别。子类别在哪里?是的,我想得到一个子类别。这是我关于如何获得子类别的子类别的问题。谢谢你的回答,但我也想得到子类别的子类别。你当前的数组是什么样子的?在问题中,我提到了当前结果的图像。你只添加了父类别和子类别,父类别是子类别,子类别是子类别。子类别在哪里?是的,我想得到一个孩子,这是我关于如何得到子类别的孩子的问题