首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > PHP >

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

2013-07-26 
请教一个多维数组递归查找问题有个无限极分类的数据表我把数据表分类,生成了多维数组进行保存function get

请教一个多维数组递归查找问题
有个无限极分类的数据表
我把数据表分类,生成了多维数组进行保存
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] =>
                                        )
                                )

                        ) 递归
[解决办法]
if(is_array($v['child']))
  $this->getkindcache($id,$v['child'],$data1);
}
这个地方没有接收返回的值
你可以写作
$data1 = array_merge($data1, $this->getkindcache($id,$v['child'],$data1));

由于是在类中,写作这样可能更好

function getkindcache($id,$str, &$data1=array()) { //$data1 是引用 


  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;
}


[解决办法]
function getkindcache($id,$str,&$data1=array())


或者if(is_array($v['child']))
$data1 = $this->getkindcache($id,$v['child'],$data1);

热点排行