SHOW:
|
|
- or go back to the newest paste.
| 1 | $list = array( | |
| 2 | 1 => (object)array( | |
| 3 | 'id' => 1, | |
| 4 | 'parent' => 0 | |
| 5 | ), | |
| 6 | 2 => (object)array( | |
| 7 | 'id' => 2, | |
| 8 | 'parent' => 0 | |
| 9 | ), | |
| 10 | 3 => (object)array( | |
| 11 | 'id' => 3, | |
| 12 | 'parent' => 2 | |
| 13 | ), | |
| 14 | 4 => (object)array( | |
| 15 | 'id' => 4, | |
| 16 | 'parent' => 2 | |
| 17 | ), | |
| 18 | 5 => (object)array( | |
| 19 | 'id' => 5, | |
| 20 | 'parent' => 2 | |
| 21 | ), | |
| 22 | 6 => (object)array( | |
| 23 | 'id' => 6, | |
| 24 | 'parent' => 5 | |
| 25 | ), | |
| 26 | ); | |
| 27 | ||
| 28 | $arr = array(); | |
| 29 | - | if ($leaf->parent !== 0) {
|
| 29 | + | |
| 30 | - | $list[$leaf->parent]->children[] = $leaf; |
| 30 | + | if ($leaf->parent !== 0) {
|
| 31 | - | unset($list[$leaf->id]); |
| 31 | + | $list[$leaf->parent]->children[] = $leaf; |
| 32 | - | } |
| 32 | + | |
| 33 | } else {
| |
| 34 | $arr[] = $leaf; | |
| 35 | - | echo "<pre>"; print_r($list); echo "</pre>"; |
| 35 | + | } |
| 36 | } | |
| 37 | ||
| 38 | echo "<pre>"; print_r($arr); echo "</pre>"; |