Recusive功能输出野生多维数组,想它一个级别深 [英] Recusive function outputting a wild multidimentional array, would like it one level deep

查看:110
本文介绍了Recusive功能输出野生多维数组,想它一个级别深的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图写钻回未知深度的窝根类别递归函数。

  [TABLE]   CAT_ID | cat_name | cat_parent | cat_slug
   //每个类别为0的cat_parent是根类[/表]

例如,SQL结果

 阵列

[0] =>排列
    (
        [CAT_ID] => 17
        [cat_name] =>另一个试验类别
        [cat_parent] => 16
        [cat_slug] =>另一个测试类别
    ))

功能:

 函数面包屑($ CAT_ID){ $ cat_nest =
            选择 *
            FROM表
            WHERE CAT_ID ='$ CAT_ID
           //返回1列;  $ cat_array [$ CAT_ID] = $ cat_nest [0];   如果($ cat_nest [0] [cat_parent']!= 0){   $ cat_array [] =面包屑($ cat_nest [0] [cat_parent']);
   }   返回$ cat_array; }

据输出:

 阵列

[17] =>排列

    [CAT_ID] => 17
    [cat_name] => test.example.1
    [cat_parent] => 16
    [cat_slug] =>测试实施例1
)[18] =>排列

    [16] =>排列
        (
            [CAT_ID] => 16
            [cat_name] => test.example.2
            [cat_parent] => 15
            [cat_slug] =>试验例2
        )    [17] =>排列
        (
            [15] =>排列
                (
                    [CAT_ID] => 15
                    [cat_name] => test.example.3
                    [cat_parent] => 6
                    [cat_slug] =>试验实施例3
                )            [16] =>排列
                (
                    [6] =>排列
                        (
                            [CAT_ID] => 6
                            [cat_name] => test.example.4
                            [cat_parent] => 2
                            [cat_slug] =>试验实施例4
                        )                    [7] =>排列
                        (
                            [2] =>排列
                                (
                                    [CAT_ID] => 2
                                    [cat_name] => test.example.5
                                    [cat_parent] => 0
                                    [cat_slug] =>试验例5
                                )                        )                )        )))

我会把它想输出

 阵列

[17] =>排列

[CAT_ID] => 17
[cat_name] => test.example.1
[cat_parent] => 16
[cat_slug] =>测试实施例1
)[16] =>排列

[CAT_ID] => 16
[cat_name] => test.example.2
[cat_parent] => 15
[cat_slug] =>试验例2

[15] =>排列

[CAT_ID] => 15
[cat_name] => test.example.3
[cat_parent] => 6
[cat_slug] =>试验实施例3

[6] =>排列

[CAT_ID] => 6
[cat_name] => test.example.4
[cat_parent] => 2
[cat_slug] =>试验实施例4

[2] =>排列

[CAT_ID] => 2
[cat_name] => test.example.5
[cat_parent] => 0
[cat_slug] =>试验例5
))


解决方案

我跑了一些测试,我认为该解决方案可以实现这种方式:

 函数面包屑($ CAT_ID){    $ cat_nest =
    选择 *
    FROM表
    WHERE CAT_ID ='$ CAT_ID
    //返回1列;    $ cat_array [$ CAT_ID] = $ cat_nest [0];    如果($ cat_nest [0] [cat_parent']!= 0){        $ cat_array = array_merge($ cat_array,面包屑($ cat_nest [0] [cat_parent']));
    }    返回$ cat_array;}

这将保持完全相同的ID,你需要他们,因为这只会是合并的阵列,而不是与创建新的索引 $ cat_array []

I am trying to write a recursive function that drills back to the root category of a nest of unknown depth.

[TABLE]

   cat_id | cat_name | cat_parent | cat_slug  
   //each category with a cat_parent of 0 is a root category

[/TABLE]

Example sql result

Array
(
[0] => Array
    (
        [cat_id] => 17
        [cat_name] => another-test-category
        [cat_parent] => 16
        [cat_slug] => Another test category
    )

)

The function:

function breadcrumb($cat_id){

 $cat_nest =     
            SELECT *
            FROM table
            WHERE cat_id = '$cat_id' 
           //returns 1 row;

  $cat_array[$cat_id] = $cat_nest[0];  

   if($cat_nest[0]['cat_parent'] != 0){

   $cat_array[] = breadcrumb($cat_nest[0]['cat_parent']);
   } 

   return $cat_array;

 }

It is outputting:

Array
(
[17] => Array
(
    [cat_id] => 17
    [cat_name] => test.example.1
    [cat_parent] => 16
    [cat_slug] => Test Example 1
)

[18] => Array
(
    [16] => Array
        (
            [cat_id] => 16
            [cat_name] => test.example.2
            [cat_parent] => 15
            [cat_slug] => Test Example 2
        )

    [17] => Array
        (
            [15] => Array
                (
                    [cat_id] => 15
                    [cat_name] => test.example.3
                    [cat_parent] => 6
                    [cat_slug] => Test Example 3
                )

            [16] => Array
                (
                    [6] => Array
                        (
                            [cat_id] => 6
                            [cat_name] => test.example.4
                            [cat_parent] => 2
                            [cat_slug] => Test Example 4
                        )

                    [7] => Array
                        (
                            [2] => Array
                                (
                                    [cat_id] => 2
                                    [cat_name] => test.example.5
                                    [cat_parent] => 0
                                    [cat_slug] => Test Example 5
                                )

                        )

                )

        )

)

)

I would like it to output

Array
(
[17] => Array
(
[cat_id] => 17
[cat_name] => test.example.1
[cat_parent] => 16
[cat_slug] => Test Example 1
)

[16] => Array
(
[cat_id] => 16
[cat_name] => test.example.2
[cat_parent] => 15
[cat_slug] => Test Example 2
)


[15] => Array
(
[cat_id] => 15
[cat_name] => test.example.3
[cat_parent] => 6
[cat_slug] => Test Example 3
)


[6] => Array
(
[cat_id] => 6
[cat_name] => test.example.4
[cat_parent] => 2
[cat_slug] => Test Example 4
)


[2] => Array
(
[cat_id] => 2
[cat_name] => test.example.5
[cat_parent] => 0
[cat_slug] => Test Example 5
)

)

解决方案

I ran some tests and I think the solution can be implemented this way:

function breadcrumb($cat_id){

    $cat_nest =     
    SELECT *
    FROM table
    WHERE cat_id = '$cat_id' 
    //returns 1 row;

    $cat_array[$cat_id] = $cat_nest[0];  

    if($cat_nest[0]['cat_parent'] != 0){

        $cat_array = array_merge( $cat_array, breadcrumb($cat_nest[0]['cat_parent']) );
    } 

    return $cat_array;

}

This would maintain the IDs exactly as you need them, since this would only be merging arrays and not creating new indexes with $cat_array[]

这篇关于Recusive功能输出野生多维数组,想它一个级别深的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆