PHP Infinite Class Classification code problem

Source: Internet
Author: User
I tried to write an infinite class classification, the results of some problems, think for a long time, feel the code is OK, but the result is not correct. Now the whole person is ignorant ....

The chestnut on the net read some, seemingly the wording is different from mine.

The array structure is as follows:

Array ([0] = = Array ([id] = 1 [name] + + code [Parent] = 0) [1] = = Array ([id] = 3 [name] + = anime [parent] + 0) [2 ] = = Array ([id] = 4 [Name] = Heal [Parent] + 3) [3] =&gt ;        Array ([id] = 5 [name] + motivational [parent] + 3) [4] = = Array        ([id] = 6 [name] = = [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] = = [Name] ] = Black technology [parent] + 8))

Where id is the category unique ID, which parent is the parent class ID
I wrote the following code:

    function all($id=0){            static $_class = null;            if(is_null($_class))                    $_class = select();  //这个得出来的就是以上的数组结构,然后赋值给了`$_class`变量            $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 class classification, the results of some problems, think for a long time, feel the code is OK, but the result is not correct. Now the whole person is ignorant ....

The chestnut on the net read some, seemingly the wording is different from mine.

The array structure is as follows:

Array ([0] = = Array ([id] = 1 [name] + + code [Parent] = 0) [1] = = Array ([id] = 3 [name] + = anime [parent] + 0) [2 ] = = Array ([id] = 4 [Name] = Heal [Parent] + 3) [3] =&gt ;        Array ([id] = 5 [name] + motivational [parent] + 3) [4] = = Array        ([id] = 6 [name] = = [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] = = [Name] ] = Black technology [parent] + 8))

Where id is the category unique ID, which parent is the parent class ID
I wrote the following code:

    function all($id=0){            static $_class = null;            if(is_null($_class))                    $_class = select();  //这个得出来的就是以上的数组结构,然后赋值给了`$_class`变量            $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]);
This line removes

In view of the fact that you are still using the recursive return, I am here to provide 2 methods, one is recursive, and the other is pointer shape.

The second may not be understandable to you for the time being, but to help you learn and to be convenient to others, here is the code:

Use recursion

// 呃,我真不忍心写出这个循环那么多遍的代码,求神解救我。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;}

Using PHP's pointer properties

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

Pass in your above array, for example, the top-most 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) );

Results

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] =& Gt                                        Array ([3] = = Array  ([id] = 3 [Parent] = =                  2                          [Text] = Sub1 [children] = = Array                                        (                                                ) )                                )                        )                )        ))
  • Related Article

    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.