I am creating an e-commerce website using laravel. I have a problem to generate dropdown select from set of array. Below is the array:
array(
[0] => Array
(
[name] => Women's Clothing
[id] => 16
[parentid] => 0
[children] => Array
(
[0] => Array
(
[name] => Tops
[id] => 411
[parentid] => 16
[children] => Array
(
[0] => Array
(
[name] => Blouse
[id] => 6556
[parentid] => 411
)
[1] => Array
(
[name] => Crop Tops
[id] => 6557
[parentid] => 411
)
)
)
[1] => Array
(
[name] => Women's Outerwear
[id] => 2262
[parentid] => 16
[children] => Array
(
[0] => Array
(
[name] => Sweaters
[id] => 6570
[parentid] => 2262
)
[1] => Array
(
[name] => Cardigans
[id] => 6571
[parentid] => 2262
)
)
)
)
)
[1] => Array
(
[name] => Health & Beauty
[id] => 129
[parentid] => 0
[children] => Array
(
[0] => Array
(
[name] => Face Make Up
[id] => 2450
[parentid] => 129
[children] => Array
(
[0] => Array
(
[name] => Powder & Compacts
[id] => 6616
[parentid] => 2450
)
[1] => Array
(
[name] => Foundation
[id] => 6617
[parentid] => 2450
)
)
)
)
)
)
How to generate dropdown select from this set of array to be like this:
<select name='select_name'>
<option value="">-- please select --</option>
<optgroup label="Women's Clothing"></label>
<option value="6556">Tops > Blouse</option>
<option value="6557">Tops > Crop Tops</option>
<option value="6570">Women's Outerwear > Sweaters</option>
<option value="6571">Women's Outerwear > Cardigans</option>
<optgroup label="Health & Beauty"></label>
<option value="6616">Face Make Up > Powder & Compacts</option>
<option value="6617">Face Make Up > Foundation</option>
</select>
Looking to the array itself, all the parentid=0 should be place in optgroup of the form select. Now the challenge part for me is, how to loop the child's name and append '>' symbol and loop until the last child. The option value should be the last child id. Please help. I really had no idea how to solve my problem.
Face Make Up > Foundationor can you have something likeFace Make Up > Foundation > Red > With sparkles?