PHP tree structure for categories and sub categories without looping a MySQL query

PHP tree structure for categories and sub categories without looping a MySQL query

We are going to see how to create PHP tree structure for parent child categories without looping a MySQL query. Some times we need to display the data in a binary tree structure. So, I am going to share a PHP Function using that you can easily create parent child category structure bypassing PHP Array.

PHP tree structure for categories and sub categories without looping a MySQL query

Let’s see how we can create the binary tree structure without looping MySQL query data.

<?php
function buildTree($items) {
    $childs = array();
    foreach($items as &$item) $childs[$item['parent_id']][] = &$item;
    unset($item);
    foreach($items as &$item) if (isset($childs[$item['id']]))
            $item['childs'] = $childs[$item['id']];
    return $childs[0];
}

Just copy and paste this function then call it by passing the PHP Array as an argument. You can easily create PHP tree structure using buildTree function.

Let’s see how we can call the function and pass the PHP Array as an argument.

$items = array(
    array('id' => 55, 'name' => 'Child Category', 'parent_id' => 2),
    array('id' => 54, 'name' => 'Child Category', 'parent_id' => 2),
    array('id' => 56, 'name' => 'third Child Category', 'parent_id' => 54),
    array('id' => 2, 'name' => 'parent Category', 'parent_id' => 0),
    array('id' => 42, 'name' => 'Child Category', 'parent_id' => 1),
    array('id' => 43, 'name' => 'Child Category', 'parent_id' => 1),
    array('id' => 1, 'name' => 'parent Category', 'parent_id' => 0),
);

$output = buildTree($items);
echo '<pre>';
print_r($output);

First of all, I create the PHP Array then call the buildTree function and pass the Array as an argument. I have print the output function will return the binary PHP tree structure output like.

Array
(
    [0] => Array
        (
            [id] => 2
            [name] => parent Category
            [parent_id] => 0
            [childs] => Array
                (
                    [0] => Array
                        (
                            [id] => 55
                            [name] => Child Category
                            [parent_id] => 2
                        )

                    [1] => Array
                        (
                            [id] => 54
                            [name] => Child Category
                            [parent_id] => 2
                            [childs] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 56
                                            [name] => third Child Category
                                            [parent_id] => 54
                                        )

                                )

                        )

                )

        )

    [1] => Array
        (
            [id] => 1
            [name] => parent Category
            [parent_id] => 0
            [childs] => Array
                (
                    [0] => Array
                        (
                            [id] => 42
                            [name] => Child Category
                            [parent_id] => 1
                        )

                    [1] => Array
                        (
                            [id] => 43
                            [name] => Child Category
                            [parent_id] => 1
                        )

                )

        )

)

As per the output, our binary PHP tree is ready for use. You can easily create the parent-child hierarchy using buildTree function.

I hope this article is helpful to you. If you have any question please write the comment. We are always ready to help you.

Related Posts

  1. MySQL value BETWEEN Two Columns in Laravel
  2. Email unique validation in Laravel ignore id on update
  3. WordPress Button Shortcode – Add button to WordPress editor
  4. WordPress shortcode – How to create shortcode in WordPress
  5. WordPress change URL in database using MySQL Query
  6. PHP remove HTML tags from string using strip_tags
  7. Enable shortcodes in widget WordPress
  8. PHP remove non-empty directory programmatically
  9. PHP File upload step by step with example
  10. Laravel 6 integrate AdminLte admin theme jeroennoten

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Search Posts

Subscribe To Newsletter

Get Early Access To New Articles, Plugins, Discount Codes And Quickly Brief Updates about tips and tricks

Join 357 other subscribers

%d bloggers like this: