PHP unlimited classification code

Source: Internet
Author: User
I tried to write an infinite classification, and the results were problematic. after thinking for a long time, I felt that the code was okay, but the results were incorrect. Now the whole person is reading some online chestnuts. it seems that the writing method is different from mine. The array structure is as follows: {code ...} where id is category only... I tried to write an infinite classification, and the results were problematic. after thinking for a long time, I felt that the code was okay, but the results were incorrect. Now the whole person is disconnected ....

I read some online chestnuts and it seems that they are written differently from me.

The array structure is as follows:

Array ([0] => Array ([id] => 1 [name] => code [parent] => 0) [1] => Array ([id] => 3 [name] => animation [parent] => 0) [2] => Array ([id] => 4 [name] => cure [parent] => 3) [3] => Array ([id] => 5 [name] => inspirational [parent] => 3) [4] => Array ([id] => 6 [name] => Machine Warfare [parent] => 3) [5] => Array ([id] => 7 [name] => Lily [parent] => 3) [6] => Array ([id] => 8 [name] => resource [parent] => 0) [7] => Array ([id] => 9 [name] => app [parent] => 8) [8] => Array ([id] => 10 [name] => software [parent] => 8) [9] => Array ([id] => 11 [name] => [parent] => 8 ))

WhereidIs the unique ID of a category,parentIs the parent class ID
The code I wrote is as follows:

Function all ($ id = 0) {static $ _ class = null; if (is_null ($ _ class) $ _ class = select (); // The above array structure is obtained, and the value is assigned to the '$ _ class' variable $ result = array (); foreach ($ _ class as $ k =>$ v) {if ($ v ['parent'] ==$ id) {unset ($ _ class [$ k]); $ v = array_merge ($ v, $ this-> all ($ v ['id']); $ result ['child '] [] = $ v ;}} return $ result;} print_r (all (0 ));

Reply content:

I tried to write an infinite classification, and the results were problematic. after thinking for a long time, I felt that the code was okay, but the results were incorrect. Now the whole person is disconnected ....

I read some online chestnuts and it seems that they are written differently from me.

The array structure is as follows:

Array ([0] => Array ([id] => 1 [name] => code [parent] => 0) [1] => Array ([id] => 3 [name] => animation [parent] => 0) [2] => Array ([id] => 4 [name] => cure [parent] => 3) [3] => Array ([id] => 5 [name] => inspirational [parent] => 3) [4] => Array ([id] => 6 [name] => Machine Warfare [parent] => 3) [5] => Array ([id] => 7 [name] => Lily [parent] => 3) [6] => Array ([id] => 8 [name] => resource [parent] => 0) [7] => Array ([id] => 9 [name] => app [parent] => 8) [8] => Array ([id] => 10 [name] => software [parent] => 8) [9] => Array ([id] => 11 [name] => [parent] => 8 ))

WhereidIs the unique ID of a category,parentIs the parent class ID
The code I wrote is as follows:

Function all ($ id = 0) {static $ _ class = null; if (is_null ($ _ class) $ _ class = select (); // The above array structure is obtained, and the value is assigned to the '$ _ class' variable $ result = array (); foreach ($ _ class as $ k =>$ v) {if ($ v ['parent'] ==$ id) {unset ($ _ class [$ k]); $ v = array_merge ($ v, $ this-> all ($ v ['id']); $ result ['child '] [] = $ v ;}} return $ result;} print_r (all (0 ));

Unset ($ _ class [$ k]);
Remove this line

Since you still use recursion to complete this process, I provide two methods here, one is recursive and the other is pointer form.

Second, you may not be able to understand it for the moment, but the following code is used to help you learn and help others:

Use recursion
// Well, I can't bear to write the code for this loop so many times, asking for help. Function _ data_to_tree (& $ items, $ topid = 0, $ with_id = TRUE) {$ result = []; foreach ($ items as $ v) if ($ topid = $ v ['parent']) {$ r = $ v + ['Children '=> _ data_to_tree ($ items, $ v ['id'], $ with_id)]; if ($ with_id) $ result [$ v ['id'] = $ r; else $ result [] = $ r;} return $ result ;}
Use PHP pointer features
function _data_to_tree($items, $topid = 0, $with_id = TRUE){    if ($with_id)        foreach ($items as $item)            $items[ $item['parent'] ]['children'][ $item['id'] ] = &$items[ $item['id'] ];    else        foreach ($items as $item)                $items[ $item['parent'] ]['children'][] = &$items[ $item['id'] ];         return isset($items[ $topid ]['children']) ? $items[ $topid ][ 'children' ] : [];}
Use

Input your above array. for example, the top-level ID is 0.

$data = [   ['id' => 4, 'parent' => 1 , 'text' => 'Parent1'],    ['id' => 1, 'parent' => 0 , 'text' => 'Root'],   ['id' => 2, 'parent' => 1 , 'text' => 'Parent2'],    ['id' => 3, 'parent' => 2 , 'text' => 'Sub1'], ];print_r ( _data_to_tree($data, 0) );
Result
Array(    [1] => Array        (            [id] => 1            [parent] => 0            [text] => Root            [children] => Array                (                    [4] => Array                        (                            [id] => 4                            [parent] => 1                            [text] => Parent1                            [children] => Array                                (                                )                        )                    [2] => Array                        (                            [id] => 2                            [parent] => 1                            [text] => Parent2                            [children] => Array                                (                                    [3] => Array                                        (                                            [id] => 3                                            [parent] => 2                                            [text] => Sub1                                            [children] => Array                                                (                                                )                                        )                                )                        )                )        ))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.