如果为false则重新运行函数

时间:2014-01-12 18:06:06

标签: php function if-statement

我正在研究采用随机值的代码,通过几项检查检查它是否正确,如果是,它将返回它。如果不是,则应该重新运行该函数,直到找到的函数。

如果随机值正确,它会将var设置为1,如下所示:$chosen = 1;然后使用while循环它将继续运行当前函数,直到它为0:

public function generate_chair($id, $optredenID)
{
    $model = new Model_Shop;

    // Get all the chairs for the given room
    $chairs = $model->check_chair($id);

    // Get an array of reserved chairs for a given event
    $reserved = $model->get_reserved($optredenID);

    do {
        // If chosen = 0, all chairs are available.
        // If chosen = 1, one of the chairs is not available
        $chosen = 0;

        // Create a new empty array
        $rand_chairs = array();

        // Pick a random chair
        $chair = array_rand($chairs);

        // Based off the amount chosen in the dorpdown, build array
        switch($_POST['AantalPlaatsen']) {
            case '1':
                array_push($rand_chairs, $chair);
            break;

            case '2':
                array_push($rand_chairs, $chair);
                array_push($rand_chairs, $chair+1);
                // Check if the chair chosen is the last chair in a row
                // If it is, check for another chair
                if($chair == 20 || $chair == 40 || $chair == 60 || $chair == 80 || $chair == 100 || $chair == 120 || $chair == 140 ||$chair == 160 ||  
                $chair == 180 || $chair == 200) {
                    $chosen = 1;
                }
            break;

            case '3':
                array_push($rand_chairs, $chair);
                array_push($rand_chairs, $chair+1);
                array_push($rand_chairs, $chair+2);
            break;

            case '4':
                array_push($rand_chairs, $chair);
                array_push($rand_chairs, $chair+1);
                array_push($rand_chairs, $chair+2);
                array_push($rand_chairs, $chair+3);
            break;
        }

        // Check if one of the random generated chairs is in the reserved array
        // If so, the chair is unavailable and new random must be generated
        if( count(array_intersect( $rand_chairs, $reserved )) != 0 ) {
            $chosen = 1;
        }

    } while ($chosen == 1);

    return $rand_chairs;

我现在想要的是什么,但我想知道是否有人对另一个问题有所了解。 如开关盒中所示,当顾客选择2把椅子并且第一把椅子在一排的末端时,它必须检查新的椅子,因为你不能将多把椅子分布在多排椅子上。

我这样做的方式是使用很多(太多)if语句。有谁知道更好的解决方案?

2 个答案:

答案 0 :(得分:0)

为什么在这里使用递归?为什么不重复一下你需要重新运行的函数部分呢?

do {
    $chosen = 0;

    // create a new empty array
    $rand_chairs = array(); 

    // Pick a random chair
    $chair = array_rand($chairs);

    ...

    foreach($rand_chairs as $key) {
        if(in_array($key, $reserved)) {
            $chosen = 1;
            break; // oops, we need to rerun
        }
    }
} while( $chosen == 1 )

这使用do-while控制结构,该控制结构至少穿过身体一次。 由于找到了密钥,break语句会提前退出while循环。

此外,您可以使用foreach

代替array_intersect()语句
if( count(array_intersect( $rand_chairs, $reserved )) != 0 ) {
    $chosen = 1;
}

答案 1 :(得分:0)

更快的算法会是这样的:你会一次运行一排座位,因为“坐在相邻行的两端”并不是真的“坐在一起”......

Boolean seatsTaken()
int startsAt(), runLength()
For each row in seatsTaken
  adjacent = 0
  seat = 0
  while not row(seat):
    ++adjacent
  if adjacent > 0:
    startsAt.push(seat-adjacent)
    runLength.push(adjacent)

完成此操作后,您将拥有所有块的数组。只选择足够大的块(runLength()> =需要)并随机选择其中一个