Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I don't know the full usecase, but it seems that a simple recursive function would do the trick here. I've used ul/li to establish the multiple levels, but that is of course not required -- for example, I also included a function that will give you a flat array with the tasks listed in order.</p> <p>First, the HTML version:</p> <pre><code>function listTasks($list, $level=0) { print '&lt;li&gt;'.$list['task_id'].' - '.$list['task_name']; if (array_key_exists('sub_task_id', $list)) { print '&lt;br&gt;&lt;ul&gt;'; foreach ($list['sub_task_id'] as $subtasks) { listTasks($subtasks, $level+1); } print '&lt;/ul&gt;'; } print '&lt;/li&gt;'; } print '&lt;ul&gt;'; foreach ($a as $tasks) { listTasks($tasks, 1); } print '&lt;/ul&gt;'; </code></pre> <p>Sample output: </p> <pre><code>1123 - Main Task 1124 - sub Task 1-1 1125 - sub Task 1-1-1 1127 - sub Task 1-2 1126 - Main Task 2 </code></pre> <p>Generate a flat array:</p> <pre><code>function listTasks($list, &amp;$output, $level=0) { $output[] = array( 'id'=&gt;$list['task_id'], 'name'=&gt;$list['task_name'], 'level'=&gt;$level ); if (array_key_exists('sub_task_id', $list)) { foreach ($list['sub_task_id'] as $subtasks) { listTasks($subtasks, $output, $level+1); } } } $output = array(); foreach ($a as $tasks) { listTasks($tasks, $output); } </code></pre> <p>Output: </p> <pre><code>Array ( [0] =&gt; Array ( [id] =&gt; 1123 [name] =&gt; Main Task [level] =&gt; 0 ) [1] =&gt; Array ( [id] =&gt; 1124 [name] =&gt; sub Task 1-1 [level] =&gt; 1 ) [2] =&gt; Array ( [id] =&gt; 1125 [name] =&gt; sub Task 1-1-1 [level] =&gt; 2 ) [3] =&gt; Array ( [id] =&gt; 1127 [name] =&gt; sub Task 1-2 [level] =&gt; 1 ) [4] =&gt; Array ( [id] =&gt; 1126 [name] =&gt; Main Task 2 [level] =&gt; 0 ) ) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload