Convert nested array to specific selection field in laravel

I have a nested array like this:

$categories = [ ['id' => 1, 'name' => 'TV & Home Theather'], ['id' => 2, 'name' => 'Tablets & E-Readers'], ['id' => 3, 'name' => 'Computers', 'children' => [ ['id' => 4, 'name' => 'Laptops', 'children' => [ ['id' => 5, 'name' => 'PC Laptops'], ['id' => 6, 'name' => 'Macbooks (Air/Pro)'] ]], ['id' => 7, 'name' => 'Desktops'], ['id' => 8, 'name' => 'Monitors'] ]], ['id' => 9, 'name' => 'Cell Phones'] ]; 

I am looking for a way in larvel or PHP to convert it to a nested combo box:

  <option value="1">TV & Home Theather</option> <option value="2">Tablets & E-Readers</option> <option value="3">Computers</option> <option value="4">Computers >> Laptops </option> <option value="5">Computers >> Laptops >> PC Laptops</option> <option value="6">Computers >> Laptops >> Macbooks (Air/Pro) </option> <option value="7">Computers >> Desktops </option> <option value="8">Computers >> Monitors </option> <option value="9">Cell Phones</option> 

So what I want, it looks like this:

enter image description here

+5
source share
2 answers

Here is a way to implement this using pure PHP with a recursive function ...

First we define an array of categories:

 $categories = [ ['id' => 1, 'name' => 'TV & Home Theather'], ['id' => 2, 'name' => 'Tablets & E-Readers'], ['id' => 3, 'name' => 'Computers', 'children' => [ ['id' => 4, 'name' => 'Laptops', 'children' => [ ['id' => 5, 'name' => 'PC Laptops'], ['id' => 6, 'name' => 'Macbooks (Air/Pro)'] ]], ['id' => 7, 'name' => 'Desktops'], ['id' => 8, 'name' => 'Monitors'] ]], ['id' => 9, 'name' => 'Cell Phones'] ]; 

Next, we define a recursive function that will pass category names to parents:

 function printCats($categories, $parent = NULL) { while ($category = array_shift($categories)) { $catName = ($parent ? $parent.' &gt;&gt; ' : '').$category['name']; print("<option value='{$category['id']}'>{$catName}</option>\n"); if (isset($category['children'])) printCats($category['children'], $catName); } } 

And finally, a call passing a category tree:

 printCats($categories); 

Output:

 <option value='1'>TV & Home Theather</option> <option value='2'>Tablets & E-Readers</option> <option value='3'>Computers</option> <option value='4'>Computers &gt;&gt; Laptops</option> <option value='5'>Computers &gt;&gt; Laptops &gt;&gt; PC Laptops</option> <option value='6'>Computers &gt;&gt; Laptops &gt;&gt; Macbooks (Air/Pro)</option> <option value='7'>Computers &gt;&gt; Desktops</option> <option value='8'>Computers &gt;&gt; Monitors</option> <option value='9'>Cell Phones</option> 
+3
source

If you are not trying to implement this yourself, I have used this package in the past.

https://github.com/atayahmet/laravel-nestable

This will take a nested array / collection and convert it to an HTML list. For documentation, an example output is given:

 <select name="categories"> <option value="1">T-shirts</option> <option value="2" selected="selected"> Red T-shirts</option> <option value="3"> Black T-shirts</option> <option value="4">Sweaters</option> <option value="5"> Red Sweaters</option> <option value="6"> Blue Sweaters</option> </select> 

Macro support is also a good feature that makes it easy to integrate routes.

+1
source

Source: https://habr.com/ru/post/1274988/


All Articles