在php5.2.3下一切正常,但在php5.1.2下写递归函数,我发现只能用for,不能用foreach,大概是此版本下的foreach还“包不住”函数,请问有什么办法吗?比如能不能重新定义一下foreach?因为我用foreach比较方便,用for的话,不但麻烦而且效率也低。//数组:
$class_cache[6] = array('Id'=>"6", 'ParentId'=>"58");
$class_cache[35] = array('Id'=>"35", 'ParentId'=>"6");
$class_cache[61] = array('Id'=>"61", 'ParentId'=>"6");
$class_cache[23] = array('Id'=>"23", 'ParentId'=>"6");//比如下面这个用foreach的函数就只能跑一趟,下去就回不来了:
//求出某类别包含的下级类别
$theallClsIds = '';
function getAllClassIds($cid){
global $theallClsIds;
global $class_cache;
if(@is_numeric($cid)){
if (empty($theallClsIds)) $theallClsIds = $cid;
foreach($class_cache as $value){
if ($value['ParentId'] == $cid){
$theId = $value['Id'];
$theallClsIds .= ','.$theId;
getAllClassIds($theId);
//continue;
}
}
}
return $theallClsIds;
}echo getAllClassIds(6);我尝试在下面加一个continue,但也无济于事,只能是用for,但我真的很想用foreach,请问有什么办法吗?多谢!

解决方案 »

  1.   

    正常时echo的结果应该是6,35,61,23,但在PHP5.1.2下却只有6,35了。
      

  2.   


    <?php
    $class_cache[6] = array('Id'=>"6", 'ParentId'=>"58");
    $class_cache[35] = array('Id'=>"35", 'ParentId'=>"6");
    $class_cache[61] = array('Id'=>"61", 'ParentId'=>"6");
    $class_cache[23] = array('Id'=>"23", 'ParentId'=>"6");//比如下面这个用foreach的函数就只能跑一趟,下去就回不来了:
    //求出某类别包含的下级类别
    function getAllClassIds($cid){
    global $class_cache;
    if($cid>0){
    foreach($class_cache as $value){
    if ($value['ParentId'] == $cid){
    $theId = $value['Id'];
    $theallClsIds .= $theId.",";
    getAllClassIds($theId);
    }
    }
    }
    $theallClsIds=substr($theallClsIds,0,-1);
    return $theallClsIds;
    }echo getAllClassIds(6); 
    ?>
      

  3.   

    那我只能说楼主,你的PHP版本有问题.到官网上重新下载,注意正确配置~~
      

  4.   

    楼主可以用这个试试看:
    <?php
    $class_cache[6] = array('Id'=>"6", 'ParentId'=>"58"); 
    $class_cache[35] = array('Id'=>"35", 'ParentId'=>"6"); 
    $class_cache[61] = array('Id'=>"61", 'ParentId'=>"6"); 
    $class_cache[23] = array('Id'=>"23", 'ParentId'=>"6"); //比如下面这个用foreach的函数就只能跑一趟,下去就回不来了: 
    //求出某类别包含的下级类别 
    $theallClsIds = ''; 
    function getAllClassIds($cid,$theallClsIds)

    global $class_cache; 
    if(@is_numeric($cid))

    if (empty($theallClsIds)) 
    $theallClsIds = $cid; 
    foreach($class_cache as $value)

    if ($value['ParentId'] == $cid)

    $theId = $value['Id']; 
    $theallClsIds .= ','.$theId; 
    getAllClassIds($theId.$theallClsIds);



    return $theallClsIds; 
    } echo getAllClassIds(6,&$theallClsIds);
    ?>
      

  5.   

    换个PHP版本有那么难吗?还是你没有权限?再说你的版本是支持foreach啊!
      

  6.   


    <?php
    $class_cache[6] = array('Id'=>"6", 'ParentId'=>"58");
    $class_cache[35] = array('Id'=>"35", 'ParentId'=>"6");
    $class_cache[61] = array('Id'=>"61", 'ParentId'=>"6");
    $class_cache[23] = array('Id'=>"23", 'ParentId'=>"6");//比如下面这个用foreach的函数就只能跑一趟,下去就回不来了:
    //求出某类别包含的下级类别
    function getAllClassIds($cid){
    global $class_cache;
    sort($class_cache);
    if($cid>0){
        for($i=0;$i<count($class_cache);$i++){
            if ($class_cache[$i]['ParentId'] == $cid){
            $theId = $class_cache[$i]['Id'];
            $theallClsIds .= $theId.",";
            getAllClassIds($theId);
            }
        }
       }
    $theallClsIds=substr($theallClsIds,0,-1);
    return $theallClsIds;
    }echo getAllClassIds(6); 
    ?>
    再试下看~~~
      

  7.   

    设置E_REPORTING全部打开了吗?全打开会有报错的,有点有用信息,我好像遇到过。
      

  8.   


    这个的确可以,但是我还是想用foreach的,用for我也知道是可以的。
      

  9.   

    不用for,只用foreach有意思吗?关键是解决问题,也许是低版本的一个BUG,楼主再争论无任何意义!
      

  10.   


    我的问题就是寻求foreach的解决方法啊,精益求精而已,不是单纯为了解决这个功能的问题,实际上在我发这个帖子之前我就用for顶上了,我只是想寻求foreach的解决方式而已。
      

  11.   

    我看了一下,alandy同学的for循环比我原来用的要高效,因为用了一个sort的缘故。不过,还有高手指点吗?foreach在此真的行不通吗?
      

  12.   

    I spent a whole day trying to resolve the same problem, until I came accross a sollution. Try the following improvements, worked for me.In the bb-settings.php version 0.8.3 line 172 find the code:if ( is_callable( 'glob' ) )
    foreach ( glob(BBPLUGINDIR . '_*.php') as $_plugin )
    require($_plugin);Replace it with:if ( is_callable( 'glob' ) ) {
    $pluginarray = glob(BBPLUGINDIR . '_*.php');
    if ($pluginarray)
    foreach ( $pluginarray as $_plugin )
    require($_plugin);
    }The source (in German): http://www.brusdeylins.info/bbpress/problem-in-bb-settings/#comment-67 
      

  13.   

    <?php
    $class_cache[6] = array('Id'=>"6", 'ParentId'=>"58");
    $class_cache[35] = array('Id'=>"35", 'ParentId'=>"6");
    $class_cache[61] = array('Id'=>"61", 'ParentId'=>"6");
    $class_cache[23] = array('Id'=>"23", 'ParentId'=>"6");
    $class_cache[26] = array('Id'=>"29", 'ParentId'=>"6");//比如下面这个用foreach的函数就只能跑一趟,下去就回不来了:
    //求出某类别包含的下级类别
    function getAllClassIds($cid){
    global $class_cache;
    if($cid>0){
        foreach($class_cache as $value){
            if ($value['ParentId'] == $cid){
            $theId = $value['Id'];
            $theallClsIds .= $theId.",";
            }
        }
    }
    return $theallClsIds;
    }echo getAllClassIds(6); 
    ?>改了下楼上的 测试可以