skip to Main Content

Please advise me for best way to loop this data.
I have data in my database like this

$data = [
    ['id' => 1, 'name' => 'Category A', 'parent_id' => null],
    ['id' => 2, 'name' => 'Subcategory A.1', 'parent_id' => 1],
    ['id' => 3, 'name' => 'Subcategory A.1.1', 'parent_id' => 2],
    ['id' => 4, 'name' => 'Subcategory A.1.2', 'parent_id' => 2],
    ['id' => 5, 'name' => 'Category B', 'parent_id' => null],
    ['id' => 6, 'name' => 'Subcategory B.1', 'parent_id' => 5],
    ['id' => 7, 'name' => 'Subcategory B.1.1', 'parent_id' => 6],
    ['id' => 8, 'name' => 'Subcategory B.1.2', 'parent_id' => 6],
    ['id' => 9, 'name' => 'Subcategory A.1.3', 'parent_id' => 2],
    ['id' => 10, 'name' => 'Subcategory A.2', 'parent_id' => 1],
    ['id' => 11, 'name' => 'Subcategory A.2.1', 'parent_id' => 10],
];

and I want to wrap the loop in a div tag like this

<div class="card">
  <div class="header">Category 1</div>
  <div class="column-start">Subcategory A.1</div>
  <div class="column">Subcategory A.1.1</div>
  <div class="column">Subcategory A.1.2</div>
  <div class="column">Subcategory A.1.3</div>
  <div class="column-start">Subcategory A.2</div>
  <div class="column">Subcategory A.2.1</div>

  <div class="header">Category 2</div>
  <div class="column-start">Subcategory B.2</div>
  <div class="column">Subcategory B.2.1</div>
  <div class="column">Subcategory B.2.2</div>
</div>

I want to do a loop so I get a result like this
enter image description here

2

Answers


  1. Try to solve problems on your own for at-least 20 minutes and then post a question here with the code that you’ve tried. However here is the solution:

    $categories = [];
    $subcategories = [];
    
    foreach ($data as $item) {
        if ($item['parent_id'] === null) {
            $categories[] = $item;
        } else {
            $subcategories[$item['parent_id']][] = $item;
        }
    } 
    
    
    echo '<div class="card">';
    
    foreach ($categories as $category) {
        echo '<div class="header">' . $category['name'] . '</div>';
        if (isset($subcategories[$category['id']])) {
            echo '<ul>';
            foreach ($subcategories[$category['id']] as $subcategory) {
                echo '<li>' . $subcategory['name'] . '</li>';
                if (isset($subcategories[$subcategory['id']])) {
                    echo '<ol type="1">';
                    foreach ($subcategories[$subcategory['id']] as $subsubcategory) {
                        echo '<li>' . $subsubcategory['name'] . '</li>';
                    }
                    echo '</ol>';
                }
            }
            echo '</ul>';
        }
    }
    
    echo '</div>';
    
    Login or Signup to reply.
  2. Try to below solution and if you’ll face any issue comment here:

    $categories = [];
    foreach ($data as $item) {
        $parentId = $item['parent_id'] ?? null;
        if (!isset($categories[$parentId])) {
            $categories[$parentId] = [];
        }
        $categories[$parentId][] = $item;
    }
    
    function buildHierarchy($categories, $parentId = null) {
        if (isset($categories[$parentId])) {
            $result = '';
            foreach ($categories[$parentId] as $item) {
                $subcategories = buildHierarchy($categories, $item['id']);
                $result .= '<div class="column';
                if (empty($subcategories)) {
                    $result .= '-start';
                }
                $result .= '">' . $item['name'] . '</div>' . $subcategories;
            }
            return $result;
        }
        return '';
    }
    
    $hierarchicalData = buildHierarchy($categories);
    
    echo '<div class="card">' . $hierarchicalData . '</div>';
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search