在递归函数中获取不正确的数组以生成随机数

时间:2016-10-13 06:17:22

标签: php arrays codeigniter

我有一个请求,它应该生成两到三个随机数,其总和不应该有结转。我已经使用stackoverflow中的一些更多引用制作了下面的代码,但是我得到了一些空数组。流程运作完美。

我只需要2位数字,其总和不应该是结转。比如25 +24 = 49而不是25 + 25,这提供了超过1的结转。

function print_questions1($i){
    if($i < 3){
        $m = 1;
        $n = 9;
        $number1 = rand($m,$n);
        $number2 = rand($m,$n);
        if (($number1+$number2)<10) {
            ${'final_num1_'.$i} =  $number1;
            ${'final_num2_'.$i} =  $number2;
            ${'final_num3_'.$i} =  0;
            $final_array = array(
                'number1_'.$i=> $number1,
                'number2_'.$i => $number2,
                'number3_'.$i => 0
            );
            return $final_array;
        }
        else
        {
            $this->print_questions1($i);
        }
    } elseif ($i > 2 && $i < 7) {
        $m = 10;
        $n = 99;
        $number1 = rand($m,$n);
        $number2 = rand($m,$n);
        $value1 = str_split($number1,1);    
        $value2 = str_split($number2,1);
        if (($value1[0]+$value2[0])<9 && ($value1[1]+$value2[1])<9) {
            ${'final_num1_0_'.$i} =  $value1[0];
            ${'final_num1_1_'.$i} =  $value1[1];
            ${'final_num2_0_'.$i} =  $value2[0];
            ${'final_num2_1_'.$i} =  $value2[1];
            ${'final_num3_0_'.$i} =  0;
            ${'final_num3_1_'.$i} =  0;
            $final_array = array(   
                'number1_0_'.$i=>  ${'final_num1_0_'.$i},
                'number1_1_'.$i=>  ${'final_num1_1_'.$i},
                'number2_0_'.$i=>  ${'final_num2_0_'.$i},
                'number2_1_'.$i=>  ${'final_num2_1_'.$i},
                'number3_0_'.$i=>  0,
                'number3_1_'.$i=>  0
            );
            return $final_array;
        }
        else
        {
            $this->print_questions1($i);
        }
    } elseif($i > 6 && $i < 10 ){
        $m = 10;
        $n = 99;
        $number1 = rand($m,$n);
        $number2 = rand($m,$n);
        $number3 = rand($m,$n);
        $value1  = str_split($number1,1);   
        $value2  = str_split($number2,1);
        $value3  = str_split($number3,1);
        if (($value1[0]+$value2[0]+$value3[0])<9 && ($value1[1]+$value2[1]+$value3[1])<9) {
            ${'final_num1_0_'.$i} =  $value1[0];
            ${'final_num1_1_'.$i} =  $value1[1];
            ${'final_num2_0_'.$i} =  $value2[0];
            ${'final_num2_1_'.$i} =  $value2[1];
            ${'final_num3_0_'.$i} =  $value3[0];
            ${'final_num3_1_'.$i} =  $value3[1];
            $final_array = array(   
                    'number1_0_'.$i=>  ${'final_num1_0_'.$i},
                    'number1_1_'.$i=>  ${'final_num1_1_'.$i},
                    'number2_0_'.$i=>  ${'final_num2_0_'.$i},
                    'number2_1_'.$i=>  ${'final_num2_1_'.$i},
                    'number3_0_'.$i=>  ${'final_num3_0_'.$i},
                    'number3_1_'.$i=>  ${'final_num3_1_'.$i}
                );
              return $final_array;
        } else {
             $this->print_questions1($i);
        }         
     }
}

function print_questions($question_type) {
    $data = array();
    for ($i=0; $i <10 ; $i++) { 
        if ($i < 3) {
                $value =  $this->print_questions1($i);               
                array_push($data, $value);


        }elseif ($i > 2 && $i < 7 ) {
            $value = $this->print_questions1($i);
            array_push($data, $value);

        }else {
            $value = $this->print_questions1($i);
            array_push($data, $value);
        }
    }

上述函数的输出提供了几个空值,如下面的数组

Array
(
    [0] => 
    [1] => Array
        (
            [number1_1] => 1
            [number2_1] => 2
            [number3_1] => 0
        )

    [2] => 
    [3] => 
    [4] => 
    [5] => 
    [6] => 
    [7] => Array
        (
            [number1] => 82
            [number2] => 83
            [number3] => 48
        )

    [8] => Array
        (
            [number1] => 31
            [number2] => 46
            [number3] => 39
        )

    [9] => Array
        (
            [number1] => 25
            [number2] => 13
            [number3] => 90
        )

)

请让我知道我的错误或我落后的地方。

1 个答案:

答案 0 :(得分:0)

我在其他部分犯了一个错误,我忘了退回这个功能。问题解决了

 function print_questions1($i){
        if($i < 3){
            $m = 1;
            $n = 9;
            $number1 = rand($m,$n);
            $number2 = rand($m,$n);
            if (($number1+$number2)<10) {
                ${'final_num1_'.$i} =  $number1;
                ${'final_num2_'.$i} =  $number2;
                ${'final_num3_'.$i} =  0;
                $final_array = array(
                    'number1_'.$i=> $number1,
                    'number2_'.$i => $number2,
                    'number3_'.$i => 0
                );
                return $final_array;
            }
            else
            {
                return $this->print_questions1($i);
            }
        } elseif ($i > 2 && $i < 7) {
            $m = 10;
            $n = 99;
            $number1 = rand($m,$n);
            $number2 = rand($m,$n);
            $value1 = str_split($number1,1);    
            $value2 = str_split($number2,1);
            if (($value1[0]+$value2[0])<9 && ($value1[1]+$value2[1])<9) {
                ${'final_num1_0_'.$i} =  $value1[0];
                ${'final_num1_1_'.$i} =  $value1[1];
                ${'final_num2_0_'.$i} =  $value2[0];
                ${'final_num2_1_'.$i} =  $value2[1];
                ${'final_num3_0_'.$i} =  0;
                ${'final_num3_1_'.$i} =  0;
                $final_array = array(   
                    'number1_0_'.$i=>  ${'final_num1_0_'.$i},
                    'number1_1_'.$i=>  ${'final_num1_1_'.$i},
                    'number2_0_'.$i=>  ${'final_num2_0_'.$i},
                    'number2_1_'.$i=>  ${'final_num2_1_'.$i},
                    'number3_0_'.$i=>  0,
                    'number3_1_'.$i=>  0
                );
                return $final_array;
            }
            else
            {
                return $this->print_questions1($i);
            }
        } elseif($i > 6 && $i < 10 ){
            $m = 10;
            $n = 99;
            $number1 = rand($m,$n);
            $number2 = rand($m,$n);
            $number3 = rand($m,$n);
            $value1  = str_split($number1,1);   
            $value2  = str_split($number2,1);
            $value3  = str_split($number3,1);
            if (($value1[0]+$value2[0]+$value3[0])<9 && ($value1[1]+$value2[1]+$value3[1])<9) {
                ${'final_num1_0_'.$i} =  $value1[0];
                ${'final_num1_1_'.$i} =  $value1[1];
                ${'final_num2_0_'.$i} =  $value2[0];
                ${'final_num2_1_'.$i} =  $value2[1];
                ${'final_num3_0_'.$i} =  $value3[0];
                ${'final_num3_1_'.$i} =  $value3[1];
                $final_array = array(   
                        'number1_0_'.$i=>  ${'final_num1_0_'.$i},
                        'number1_1_'.$i=>  ${'final_num1_1_'.$i},
                        'number2_0_'.$i=>  ${'final_num2_0_'.$i},
                        'number2_1_'.$i=>  ${'final_num2_1_'.$i},
                        'number3_0_'.$i=>  ${'final_num3_0_'.$i},
                        'number3_1_'.$i=>  ${'final_num3_1_'.$i}
                    );
                  return $final_array;
            } else {
                 return $this->print_questions1($i);
            }         
         }
    }