Reputation: 335
I have an array, and I want to make it to nested order list with php using foreach loop and make it recursive function so it unlimited order list depth. can someone help me to fix the code so it will show nested order list, any help would be appreciated
here's the sample array
Array
(
[_code] => Ok
[title] => Testing Dynalist API
[nodes] => Array
(
[0] => Array
(
[id] => root
[content] => Testing Dynalist API
[note] =>
[children] => Array
(
[0] => DMUvwdvZyOephz3yaXEzoW2H
[1] => fAcpRk_-daRbuc1PHyshZLVg
[2] => dWSRRQNxKRAqwMRA-PX0rBh2
)
[created] => 1569406876611
[modified] => 1569576518922
)
[1] => Array
(
[id] => DMUvwdvZyOephz3yaXEzoW2H
[content] => create schedule dated tomorrow !(2019-09-26 +07:00)
[note] =>
[children] => Array
(
[0] => _IWo9oUw9MYjP7uhwbjHBzlC
[1] => vrh4Q0J_B2CyGy_6xSiN-MKd
)
[created] => 1569407069527
[modified] => 1569568441124
)
[2] => Array
(
[id] => _IWo9oUw9MYjP7uhwbjHBzlC
[content] => sub list
[note] =>
[created] => 1569407081410
[modified] => 1569575905167
)
[3] => Array
(
[id] => vrh4Q0J_B2CyGy_6xSiN-MKd
[content] => another sublist
[note] =>
[children] => Array
(
[0] => BlYw6xmYQ77Bg_G123Fw5ptl
)
[created] => 1569568447412
[modified] => 1569568453923
)
[4] => Array
(
[id] => BlYw6xmYQ77Bg_G123Fw5ptl
[content] => **sublist**
[note] =>
[created] => 1569568451820
[modified] => 1569575479229
)
[5] => Array
(
[id] => fAcpRk_-daRbuc1PHyshZLVg
[content] => Testing !(2019-09-28)
[note] =>
[created] => 1569571374637
[modified] => 1569571398548
)
[6] => Array
(
[id] => dWSRRQNxKRAqwMRA-PX0rBh2
[content] => write something below this
[note] =>
[created] => 1569575907404
[modified] => 1569575947174
)
)
[version] => 43
)
here's my current php code
echo makelist($array);
function makelist($array, $aschild = 0){
if(!is_array($array)) return '';
$output = '<ul>';
foreach($array[nodes] as $item){
if($aschild == 1) {
$output .= '<li>---' . $item[content];
} else {
$output .= '<li>' . $item[content];
}
if(array_key_exists('children', $item)) {
$output .= makelist($item[children],1);
} else {
$output .= "....";
}
$output .= '</li>';
}
$output .= '</ul>';
return $output;
}
Upvotes: 0
Views: 49
Reputation: 57121
There are quite a few problems with your code (syntax errors etc.) so I've written something which may also simplify the code.
The first part is to index the nodes by the id (using array_column()
) so that you can directly access each element by that id when needed. Then pass into the new makeList()
function the list of nodes, the root node and lastly a flag to say this is the root node and needs extra tags(see code for details)...
// Rebuild nodes array so that it is indexed by id
$nodes = array_column($array['nodes'], null, 'id');
echo makelist($nodes, $nodes['root'], true);
function makeList ( array $nodes, $base, $root = false ) {
// Start by adding start points content
$output = "<li>{$base['content']}</li>";
// If there are sub elements
if ( isset($base['children']) ) {
$output .= "<ul>";
foreach ( $base['children'] as $child ) {
// Add in child elements
$output .= makeList($nodes, $nodes[$child]);
}
$output .= "</ul>";
}
// If the root node, add in wrapping tags
if ( $root ) {
$output = "<ul>$output</ul>";
}
return $output ;
}
The output (with your data and formatting) gives...
<ul>
<li>Testing Dynalist API</li>
<ul>
<li>create schedule dated tomorrow !(2019-09-26 +07:00)</li>
<ul>
<li>sub list</li>
<li>another sublist</li>
<ul>
<li>**sublist**</li>
</ul>
</ul>
<li>Testing !(2019-09-28)</li>
<li>write something below this</li>
</ul>
</ul>
Upvotes: 1