app/Category.php
/**
     * Recursive function to linearize and retrieve depth of categories element in array
     *
     * @param array $categories Array with categories to process
     * @param integer $depth Level of depth
     * @param array $linearizeArray Array with partial linearized element during the recursive loop
     * @return array $linearizeArray  [ id , name, depth ]
     */
Recursive function to linearize and retrieve depth of categories element in array
@param array $categories Array with categories to process
@param integer $depth Level of depth
@param array $linearizeArray Array with partial linearized element during the recursive loop
@return array $linearizeArray  [ id , name, depth ]
Recursive function to linearize and retrieve depth of categories element in array @param array $categories Array with categories to process @param integer $depth Level of depth @param array $linearizeArray Array with partial linearized element during the recursive loop @return array $linearizeArray [ id name depth ]
public function linearizeCategoryArray($categories, $depth=0, $linearizeArray = null) {
        foreach ($categories as $key=>$category) {
            $linearizeArray[]=[
                 'id' => $category['id'],
                 'name' => $category['name'],
                 'depth'=>$depth,
            ];
            if( count( $category['children'] ) > 0 ){
                $linearizeArray = $this->linearizeCategoryArray( $category['children'], ($depth + 1), $linearizeArray );
            }
        }
        if (count($linearizeArray) > 0) {
            return $linearizeArray;
        } else {
            return array();
        }
    }
public function linearizeCategoryArray ( $ categories , $ depth = 0 , $ linearizeArray = null ) { foreach ( $ categories as $ key => $ category ) { $ linearizeArray [ ] = [ 'id' => $ category [ 'id' ] , 'name' => $ category [ 'name' ] , 'depth' => $ depth , ] ; if ( count ( $ category [ 'children' ] ) > 0 ) { $ linearizeArray = $ this -> linearizeCategoryArray ( $ category [ 'children' ] , ( $ depth + 1 ) , $ linearizeArray ) ; } } if ( count ( $ linearizeArray ) > 0 ) { return $ linearizeArray ; } else { return array ( ) ; } }
Recursive function to linearize and retrieve depth of categories element in array
 @param array $categories Array with categories to process
 @param integer $depth Level of depth
 @param array $linearizeArray Array with partial linearized element during the recursive loop
 @return array $linearizeArray  [ id , name, depth ]
Recursive function to linearize and retrieve depth of categories element in array @param array $categories Array with categories to process @param integer $depth Level of depth @param array $linearizeArray Array with partial linearized element during the recursive loop @return array $linearizeArray [ id name depth ]