Home  >  Article  >  Backend Development  >  请教一个多维数组递归查找问题

请教一个多维数组递归查找问题

WBOY
WBOYOriginal
2016-06-23 14:16:21784browse

有个无限极分类的数据表
我把数据表分类,生成了多维数组进行保存
function getkind($id)
{
if(file_exists($_G['cache'].'kindcache.txt')) //分类缓存是否存在
$str = unserialize(file_get_contents($_G['cache'].'kindcache.txt'));
else
$str = $cache->outkindcache(0); // 生成分类缓存
$data=$this->getkindcache($id,$str); //查找数组
return $data;
}

function getkindcache($id,$str,$data1=array())//递归函数
{
    global $_G;
    foreach($str as $k=>$v)
    {
      if($v['kind'] == $id)
      {
 $data1[] = $v;
       }
if(is_array($v['child']))
$this->getkindcache($id,$v['child'],$data1);

      }
      return $data1;
}
上面的这个递归函数总是返回NULL,不能保存我要返回的数据

标红的是我要查找的数据

Array
(
    [0] => Array
        (
            [uid] => 2
            [kind] => 0
            [top] => 2
            [pagesize] => 15
            [name] => 新闻
            [sort] => 1
            [keywords] => 
            [description] => 
            [enable] => 1
            [html] => 1
            [outlink] => 
            [model] => news
            [modelname] => 新闻模型
            [template] => news.html
            [templist] => news_list.html
            [tempview] => news_view.html
            [typedir] => http://localhost/2011-4-30/static/2
            [grade] => 1
            [kindtype] => 1
            [child] => Array
                (
                    [0] => Array
                        (
                            [uid] => 1
                             [kind] => 2
                            [top] => 1
                            [pagesize] => 15
                            [name] => 产品展示
                            [sort] => 0
                            [keywords] => 
                            [description] => 
                            [enable] => 1
                            [html] => 1
                            [outlink] => 
                            [model] => product
                            [modelname] => 产品模型
                            [template] => product.html
                            [templist] => product_list.html
                            [tempview] => product_view.html
                            [typedir] => http://localhost/2011-4-30/static/1
                            [grade] => 1
                            [kindtype] => 1
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [uid] => 15
                                            [kind] => 1
                                            [top] => 1
                                            [pagesize] => 15
                                            [name] => 橡胶制品
                                            [sort] => 0
                                            [keywords] => 
                                            [description] => 
                                            [enable] => 1
                                            [html] => 1
                                            [outlink] => 
                                            [model] => product
                                            [modelname] => 产品模型
                                            [template] => product.html
                                            [templist] => product_list.html
                                            [tempview] => product_view.html
                                            [typedir] => http://localhost/2011-4-30/static/1/15/list_1.html
                                            [grade] => 1
                                            [kindtype] => 0
                                            [child] => 
                                        )
                                )

                        )

                    [1] => Array
                        (
                            [uid] => 3
                             [kind] => 2
                            [top] => 3
                            [pagesize] => 30
                            [name] => 图片展示
                            [sort] => 0
                            [keywords] => 
                            [description] => 
                            [enable] => 1
                            [html] => 1
                            [outlink] => 
                            [model] => images
                            [modelname] => 图片模型
                            [template] => images.html
                            [templist] => images_list.html
                            [tempview] => images_view.html
                            [typedir] => http://localhost/2011-4-30/static/3/list_1.html
                            [grade] => 1
                            [kindtype] => 0
                            [child] => Array
                                (
                                    [0] => Array
                                        (
                                            [uid] => 17
                                            [kind] => 3
                                            [top] => 3
                                            [pagesize] => 15
                                            [name] => 测试
                                            [sort] => 0
                                            [keywords] => 1111
                                            [description] => 2222
                                            [enable] => 1
                                            [html] => 1
                                            [outlink] => 
                                            [model] => news
                                            [modelname] => 新闻模型
                                            [template] => news.html
                                            [templist] => news_list.html
                                            [tempview] => news_view.html
                                            [typedir] => http://localhost/2011-4-30/static/3/17/list_1.html
                                            [grade] => 0
                                            [kindtype] => 0
                                            [child] => 
                                        )

                                    [1] => Array
                                        (
                                            [uid] => 18
                                            [kind] => 3
                                            [top] => 3
                                            [pagesize] => 15
                                            [name] => 测试
                                            [sort] => 0
                                            [keywords] => 
                                            [description] => 
                                            [enable] => 1
                                            [html] => 1
                                            [outlink] => 
                                            [model] => news
                                            [modelname] => 新闻模型
                                            [template] => news.html
                                            [templist] => news_list.html
                                            [tempview] => news_view.html
                                            [typedir] => http://localhost/2011-4-30/static/3/18/list_1.html
                                            [grade] => 0
                                            [kindtype] => 0
                                            [child] => 
                                        )

                                    [2] => Array
                                        (
                                            [uid] => 19
                                            [kind] => 3
                                            [top] => 3
                                            [pagesize] => 15
                                            [name] => 测试
                                            [sort] => 0
                                            [keywords] => 
                                            [description] => 
                                            [enable] => 1
                                            [html] => 1
                                            [outlink] => 
                                            [model] => news
                                            [modelname] => 新闻模型
                                            [template] => news.html
                                            [templist] => news_list.html
                                            [tempview] => news_view.html
                                            [typedir] => http://localhost/2011-4-30/static/3/19/list_1.html
                                            [grade] => 0
                                            [kindtype] => 0
                                            [child] =>
                                        )
                                )

                        )


回复讨论(解决方案)

  [3] => Array
                        (
                            [uid] => 7
                             [kind] => 2
                            [top] => 2
                            [pagesize] => 15
                            [name] => 法国新闻
                            [sort] => 0
                            [keywords] => 
                            [description] => 
                            [enable] => 1
                            [html] => 1
                            [outlink] => 
                            [model] => news
                            [modelname] => 新闻模型
                            [template] => news.html
                            [templist] => news_list.html
                            [tempview] => news_view.html
                            [typedir] => http://localhost/2011-4-30/static/2/7
                            [grade] => 1
                            [kindtype] => 1
                            [child] =>
                                )

                        )

                    [4] => Array
                        (
                            [uid] => 8
                             [kind] => 2
                            [top] => 2
                            [pagesize] => 15
                            [name] => 中国新闻
                            [sort] => 0
                            [keywords] => 
                            [description] => 
                            [enable] => 1
                            [html] => 1
                            [outlink] => 
                            [model] => news
                            [modelname] => 新闻模型
                            [template] => news.html
                            [templist] => news_list.html
                            [tempview] => news_view.html
                            [typedir] => http://localhost/2011-4-30/static/2/8/list_1.html
                            [grade] => 1
                            [kindtype] => 0
                            [child] => 
                        )

                    [5] => Array
                        (
                            [uid] => 9
                             [kind] => 2
                            [top] => 2
                            [pagesize] => 15
                            [name] => 俄罗斯新闻
                            [sort] => 0
                            [keywords] => 
                            [description] => 
                            [enable] => 1
                            [html] => 1
                            [outlink] => 
                            [model] => news
                            [modelname] => 新闻模型
                            [template] => news.html
                            [templist] => news_list.html
                            [tempview] => news_view.html
                            [typedir] => http://localhost/2011-4-30/static/2/9/list_1.html
                            [grade] => 1
                            [kindtype] => 0
                            [child] => 
                        )

                    [6] => Array
                        (
                            [uid] => 10
                             [kind] => 2
                            [top] => 2
                            [pagesize] => 15
                            [name] => 英国新闻
                            [sort] => 0
                            [keywords] => 
                            [description] => 
                            [enable] => 1
                            [html] => 1
                            [outlink] => 
                            [model] => news
                            [modelname] => 新闻模型
                            [template] => news.html
                            [templist] => news_list.html
                            [tempview] => news_view.html
                            [typedir] => http://localhost/2011-4-30/static/2/10/list_1.html
                            [grade] => 1
                            [kindtype] => 0
                            [child] => 
                        )

                )

        )

    [1] => Array
        (
            [uid] => 4
            [kind] => 0
            [top] => 4
            [pagesize] => 15
            [name] => 信息分类
            [sort] => 6
            [keywords] => 
            [description] => 
            [enable] => 1
            [html] => 1
            [outlink] => 
            [model] => info
            [modelname] => 分类模型
            [template] => info.html
            [templist] => info_list.html
            [tempview] => info_view.html
            [typedir] => http://localhost/2011-4-30/static/4/list_1.html
            [grade] => 1
            [kindtype] => 0
            [child] => Array
                (
                    [0] => Array
                        (
                            [uid] => 5
                            [kind] => 4
                            [top] => 4
                            [pagesize] => 15
                            [name] => 旅游摄影
                            [sort] => 0
                            [keywords] => 旅游摄影
                            [description] => 旅游摄影
                            [enable] => 1
                            [html] => 1
                            [outlink] => 
                            [model] => info
        

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn