SpongeBobPHPants
SpongeBobPHPants

Reputation: 681

Recurse through a multi-dimensional PHP array to produce a HTML list

Sadly I struggle with anything where recursion is involved and this problem is no different.

I have a category structure in a PHP array. I have grouped it by parent categories like this, but if a solution warrants it, I would be happy to change that:

array(5) {
  [0] => array(4) {
    [50] => array(5) {
      ["category_id"] => int(50)
      ["category_title"] => string(29) "Category 50"
      ["parent_category_id"] => int(0)
      ["display_order"] => int(0)
      ["depth"] => int(0)
    }
    [1] => array(5) {
      ["category_id"] => int(1)
      ["category_title"] => string(24) "Category 1"
      ["parent_category_id"] => int(0)
      ["display_order"] => int(1)
      ["depth"] => int(0)
    }
    [80] => array(5) {
      ["category_id"] => int(80)
      ["category_title"] => string(27) "Category 80"
      ["parent_category_id"] => int(0)
      ["display_order"] => int(1)
      ["depth"] => int(0)
    }
    [10] => array(5) {
      ["category_id"] => int(10)
      ["category_title"] => string(29) "Category 10"
      ["parent_category_id"] => int(0)
      ["display_order"] => int(10)
      ["depth"] => int(0)
    }
  }
  [1] => array(1) {
    [2] => array(5) {
      ["category_id"] => int(2)
      ["category_title"] => string(21) "Category 2"
      ["parent_category_id"] => int(1)
      ["display_order"] => int(1)
      ["depth"] => int(1)
    }
  }
  [30] => array(1) {
    [90] => array(5) {
      ["category_id"] => int(90)
      ["category_title"] => string(11) "Category 90"
      ["parent_category_id"] => int(30)
      ["display_order"] => int(1)
      ["depth"] => int(2)
    }
  }
  [10] => array(2) {
    [20] => array(5) {
      ["category_id"] => int(20)
      ["category_title"] => string(21) "Category 20"
      ["parent_category_id"] => int(10)
      ["display_order"] => int(10)
      ["depth"] => int(1)
    }
    [30] => array(5) {
      ["category_id"] => int(30)
      ["category_title"] => string(17) "Category 30"
      ["parent_category_id"] => int(10)
      ["display_order"] => int(20)
      ["depth"] => int(1)
    }
  }
  [50] => array(3) {
    [40] => array(5) {
      ["category_id"] => int(40)
      ["category_title"] => string(6) "Category 40"
      ["parent_category_id"] => int(50)
      ["display_order"] => int(1000)
      ["depth"] => int(1)
    }
    [60] => array(5) {
      ["category_id"] => int(60)
      ["category_title"] => string(6) "Category 60"
      ["parent_category_id"] => int(50)
      ["display_order"] => int(2000)
      ["depth"] => int(1)
    }
    [70] => array(5) {
      ["category_id"] => int(70)
      ["category_title"] => string(17) "Category 70"
      ["parent_category_id"] => int(50)
      ["display_order"] => int(3000)
      ["depth"] => int(1)
    }
}

So the end result of the HTML ought to look like this:

<ul>
    <li>Category 50</li>
    <ul>
        <li>Category 40</li>
        <li>Category 60</li>
        <li>Category 70</li>
    </ul>
    <li>Category 1</li>
    <ul>
        <li>Category 2</li>
    </ul>
    <li>Category 80</li>
    <li>Category 10</li>
    <ul>
        <li>Category 20</li>
        <li>Category 30</li>
        <ul>
            <li>Category 90</li>
        </ul>
    </ul>
</ul>

Note that there could be multiple levels of nesting.

Although not depicted in the above example, it would be useful to be able to use more than just 'category_title' for each list item (there are some other parameters not shown, e.g. counts and other stats that have been removed for simplicity).

I am incredibly appreciative that you have even read this far. Any suggestions gratefully received.

Visual representation of the end result HTML:

I am looking to convert this into a HTML list like this:

Upvotes: 0

Views: 41

Answers (1)

Mihai Matei
Mihai Matei

Reputation: 24276

This should work. Note that this is an untested script:

function recursiveList($array) {
   echo '<ul>';
   foreach ($array as $key => $value) {
       if (is_array($value) && !isset($value['category_id'])) { // if isset fails means that there is an array containing other categories arrays
           recursiveList($value);
       }
       echo '<li>' . $value['category_title'] . '</li>';
   }
   echo '</ul>';
}

recursiveList($categories);

Upvotes: 1

Related Questions