-1

我正在尝试添加循环,但出现错误...我试图在每个循环之前清除数组,但对我不起作用。

<?php

for ($aw = 1; $aw <= 49; $aw++) {

    $cart = array("orange", "banana");

    $result = array(); 
    $combination = array();

    function combinations(array $myArray, $choose) {
        global $result, $combination;

        $n = count($myArray);
        function inner ($start, $choose_, $arr, $n) {
            global $result, $combination;

            if ($choose_ == 0) array_push($result,$combination);
            else for ($i = $start; $i <= $n - $choose_; ++$i) {
                array_push($combination, $arr[$i]);
                inner($i + 1, $choose_ - 1, $arr, $n);
                array_pop($combination);
            }
        }
        inner(0, $choose, $myArray, $n);
        return $result;
    }
    $array=combinations($cart, 6); // this is your 2d array

    $counnumber=1;
    foreach ($array as $row) {
        $counnumber++;
    }
    echo $counnumber;
}

我收到以下错误

Fatal error: Cannot redeclare combinations() (previously declared in    
/home/www/mywebsite.com/checkingerror.php:11) in 
/home/www/mywebsite.com/checkingerror.php on line 11

请检查以下代码,并帮助我!

谢谢

4

2 回答 2

1

您正在combinations循环内声明函数。将其移出循环。

function combinations(array $myArray, $choose) {
global $result, $combination;

 $n = count($myArray);
 function inner ($start, $choose_, $arr, $n) {
   global $result, $combination;

    if ($choose_ == 0) array_push($result,$combination);
    else for ($i = $start; $i <= $n - $choose_; ++$i) {
       array_push($combination, $arr[$i]);
       inner($i + 1, $choose_ - 1, $arr, $n);
       array_pop($combination);
       }
  }
  inner(0, $choose, $myArray, $n);
  return $result;
}

for ($aw = 1; $aw <= 49; $aw++) {
...
于 2013-08-02T01:14:39.933 回答
1

看错误,当你第一次调用循环时,你声明了函数。

之后,每次在循环中,您都在重新定义相同的函数 =>错误

// function declaration here
// you can call the function inside the loop here
于 2013-08-02T01:15:56.210 回答