在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,请问有什么办法吗?多谢!
$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,请问有什么办法吗?多谢!
<?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);
?>
<?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);
?>
<?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);
?>
再试下看~~~
这个的确可以,但是我还是想用foreach的,用for我也知道是可以的。
我的问题就是寻求foreach的解决方法啊,精益求精而已,不是单纯为了解决这个功能的问题,实际上在我发这个帖子之前我就用for顶上了,我只是想寻求foreach的解决方式而已。
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
$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);
?>改了下楼上的 测试可以