Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php tree function parrent_id

<?php

namespace AppServices;

use AppModelsCategoryModel;

class CategoryService
{
    
    public function getTree(): array
    {
        $categories = CategoryModel::query()->orderBy('sort_category')
            ->select(['id', 'title', 'slug', 'image','parent_id'])
            ->get()->toArray();
        return $this->generateTree($categories);
    }

    public function generateTree($elements, $parentId = 0): array
    {
        $result = [];
        foreach ($elements as $element) {
            if ($element['parent_id'] == $parentId) {
                $children = $this->generateTree($elements, $element['id']);
                if ($children) {
                    $element['children'] = $children;
                }
                $result[$element['id']] = $element;
                unset($elements[$element['id']]);
            }
        }
        return $result;
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: specific function to Unflatten array 
Php :: check website ssl certificate using php openssl_x509_parse 
Php :: symfony create form multiple entities 
Php :: check input value is not empty or spaced php 
Php :: use varable on all site pages laravel 
Php :: How to hide Directory Browsing in WordPress from server? 
Php :: supprimer un cookie avec un input en php 
Php :: yii1 anchor tag 
Php :: how to write double values in phpmyadmin 
Php :: detect mobile device laravel 
Php :: run drush command from php 
Php :: registration welcome email laravel 
Php :: php unit testAdd Answer 
Php :: codeigniter 4 query builder select 
Php :: remove index.php 
Php :: how to create a modal in php 
Php :: php get site metat tags 
Php :: find string lenght in php 
Php :: Laravael Blog Project 
Php :: add taxonomy to custom post type 
Php :: can i install php7.4 inside vagrant homestead 
Php :: views_pre_view 
Java :: set size of a jframe 
Java :: setting up javafx in eclipse vm argument 
Java :: bukkit delayed task 
Java :: android open browser 
Java :: add retrofit dependency android 
Java :: install java in wsl2 
Java :: java count files in folder 
Java :: from string to int android studio 
ADD CONTENT
Topic
Content
Source link
Name
1+8 =