Вывод php многомерного массива в список html

У меня есть следующий набор массивов в php-скрипте, и я пытаюсь получить его для вывода в соответствии с приведенным ниже кодом. Я пробовал цикл, но я не понимаю, как получить его, чтобы показать вспомогательные массивы. Я надеюсь, что кто-то может мне помочь.

PHP-массив

$fruits = array(); $fruits[] = array('type' => 'Banana', 'code' => 'ban00'); $fruits[] = array('type' => 'Grape', 'code' => 'grp01'); $fruits[] = array('type' => 'Apple', 'code' => 'apl00', array('subtype' => 'Green', 'code' => 'apl00gr'), array('subtype' => 'Red', 'code' => 'apl00r') ); $fruits[] = array('type' => 'Lemon', 'code' => 'lem00'); 

Желаемый результат

 <ul> <li><input type="checkbox" name="fruit" value="Banana"> Banana</li> <li><input type="checkbox" name="fruit" value="Grape"> Grape</li> <li>Apple <ul> <li><input type="checkbox" name="fruit" value="Green"> Green</li> <li><input type="checkbox" name="fruit" value="Red"> Red</li> </ul> </li> <li><input type="checkbox" name="fruit" value="Lemon"> Lemon</li> </ul> , <ul> <li><input type="checkbox" name="fruit" value="Banana"> Banana</li> <li><input type="checkbox" name="fruit" value="Grape"> Grape</li> <li>Apple <ul> <li><input type="checkbox" name="fruit" value="Green"> Green</li> <li><input type="checkbox" name="fruit" value="Red"> Red</li> </ul> </li> <li><input type="checkbox" name="fruit" value="Lemon"> Lemon</li> </ul> 

Вы можете использовать рекурсивную функцию. Обратите внимание, что это пример, а не прямое решение, так как мы здесь, чтобы учиться и не выполнять работу – измените это по мере необходимости.

 function renderList(array $data) { $html = '<ul>'; foreach ($data as $item) { $html .= '<li>'; foreach ($item as $key => $value) { if (is_array($value)) { $html .= renderList($value); } else { $html .= $value; } } $html .= '</li>'; } $html .= '</ul>'; return $html; } $data = array(); $data[] = array('A'); $data[] = array('B', array(array('C'))); $data[] = array('D'); echo renderList($data); 

Результатом этого будет:

  • В
    • С
  • D

Или в форме html:

 <ul> <li>A</li> <li>B <ul> <li>C</li> </ul> </li> <li>D</li> </ul> 

Вам нужно сделать что-то вроде этого. Я не пытался вас закодировать, просто попытался дать вам идею. Надеюсь, вы сможете продолжить.

 echo '<ul>'; foreach( $fruits as $fruit){ echo '<li>' . $fruit . '</li>'; if( is_array($fruit) ){ echo '<ul>'; foreach( $fruit as $fr ){ echo '<li>' . $fr . '</li>'; } echo '</ul>'; } }echo '</ul>'; } 
 <?php $fruits = array(); $fruits[] = array( 'type' => 'Banana', 'code' => 'ban00' ); $fruits[] = array( 'type' => 'Grape', 'code' => 'grp01' ); $fruits[] = array( 'type' => 'Apple', 'code' => 'apl00', array( 'subtype' => 'Green', 'code' => 'apl00gr'), array( 'subtype' => 'Red', 'code' => 'apl00r' ) ); $fruits[] = array( 'type' => 'Lemon', 'code' => 'lem00' ); iterate($fruits, 'type'); function iterate($fruits, $index) { foreach ($fruits as $fruit) { echo "<ul>"; display($fruit, $index); echo "</ul>"; } } function display($data, $index) { echo '<li><input type="checkbox" name="fruit" value="' . $data[$index] . '">' . $data[$index]; if (!empty($data[0]) && is_array($data[0])) { $newSubArray = $data; unset($newSubArray['type']); unset($newSubArray['code']); iterate($newSubArray, 'subtype'); } echo "</li>"; } с <?php $fruits = array(); $fruits[] = array( 'type' => 'Banana', 'code' => 'ban00' ); $fruits[] = array( 'type' => 'Grape', 'code' => 'grp01' ); $fruits[] = array( 'type' => 'Apple', 'code' => 'apl00', array( 'subtype' => 'Green', 'code' => 'apl00gr'), array( 'subtype' => 'Red', 'code' => 'apl00r' ) ); $fruits[] = array( 'type' => 'Lemon', 'code' => 'lem00' ); iterate($fruits, 'type'); function iterate($fruits, $index) { foreach ($fruits as $fruit) { echo "<ul>"; display($fruit, $index); echo "</ul>"; } } function display($data, $index) { echo '<li><input type="checkbox" name="fruit" value="' . $data[$index] . '">' . $data[$index]; if (!empty($data[0]) && is_array($data[0])) { $newSubArray = $data; unset($newSubArray['type']); unset($newSubArray['code']); iterate($newSubArray, 'subtype'); } echo "</li>"; } с <?php $fruits = array(); $fruits[] = array( 'type' => 'Banana', 'code' => 'ban00' ); $fruits[] = array( 'type' => 'Grape', 'code' => 'grp01' ); $fruits[] = array( 'type' => 'Apple', 'code' => 'apl00', array( 'subtype' => 'Green', 'code' => 'apl00gr'), array( 'subtype' => 'Red', 'code' => 'apl00r' ) ); $fruits[] = array( 'type' => 'Lemon', 'code' => 'lem00' ); iterate($fruits, 'type'); function iterate($fruits, $index) { foreach ($fruits as $fruit) { echo "<ul>"; display($fruit, $index); echo "</ul>"; } } function display($data, $index) { echo '<li><input type="checkbox" name="fruit" value="' . $data[$index] . '">' . $data[$index]; if (!empty($data[0]) && is_array($data[0])) { $newSubArray = $data; unset($newSubArray['type']); unset($newSubArray['code']); iterate($newSubArray, 'subtype'); } echo "</li>"; }