似乎还是没有理解你的意思function &test( $id = 1 ){
  $class = array( "id"=>$id );
  if($id<10){
  return $class;
  }
}$id = 0;
foreach ($class = &test( $id ) as $key => $value){
echo $key.'=>'.$value;
$id++;
}

解决方案 »

  1.   

    1、不要“随便”,函数的表现与上下文有关。虽然可以简化,但是核心的控制不能都掉
    2、不知道你那个“随便”函数是作什么的
    相近的可能是
    function test($id=1){
      static $class;
      $class[] = array("id"=>$id);
      if($id<10){
        return $class;
      }
      return test($id-1);
    }print_r(test(13));
    out:
    Array
    (
        [0] => Array
            (
                [id] => 13
            )    [1] => Array
            (
                [id] => 12
            )    [2] => Array
            (
                [id] => 11
            )    [3] => Array
            (
                [id] => 10
            )    [4] => Array
            (
                [id] => 9
            ))3、静态变量就是为了在函数中保存前次调用时的数据,否则就不必要使用了
    4、恢复静态变量的初值,需要显式的书写逻辑。比如
    function test($id=1){
      static $class;
      if($id < 0) {
        $calss = array();
        return $class;
      }
      $class[] = array("id"=>$id);
      if($id<10){
        return $class;
      }
      return test($id-1);
    }