Php 如果为false,则重新运行函数

Php 如果为false,则重新运行函数,php,function,if-statement,Php,Function,If Statement,我正在编写一个随机值的代码,通过一些检查检查它是否正确,如果正确,它将返回它。如果没有,则应该重新运行该函数,直到找到一个函数为止 如果随机值是正确的,它将设置一个var为1,如下所示:$selected=1;然后使用while循环,它将继续运行当前函数,直到达到0: public function generate_chair($id, $optredenID) { $model = new Model_Shop; // Get all the chairs for the

我正在编写一个随机值的代码,通过一些检查检查它是否正确,如果正确,它将返回它。如果没有,则应该重新运行该函数,直到找到一个函数为止

如果随机值是正确的,它将设置一个var为1,如下所示:$selected=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语句。有人知道更好的解决方案吗?

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

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循环,因为已经找到了键

此外,可以使用array_intersect代替foreach语句:


更快的算法是这样的:您可以一次运行一排座位,因为坐在相邻座位的两端并不是真的坐在一起

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>=所需的块,并随机选择其中一个块

使用while to loop直到选择的值为0。如果包含更多的代码,将更容易帮助您。现在看来,这个问题对我来说太模糊了。你可以用十几种不同的方式来回答它。@kuroineko我已经更新了帖子以包含整个功能。看起来你在寻找一个随机的地方,在那里你可以有n个座位。找到数组中有空座位块的所有位置,然后随机选择其中一个,这样会更有效。当剧院开始人满为患时,这种算法会变得非常缓慢……弗洛里斯提出了一个很好的观点;我写了一个你的问题的答案,但是你应该考虑一个不同的算法。这似乎是可行的,但是在几次而不是一个数组之后,它只加载一段时间,然后返回一个空的屏幕。这似乎只有在$selected==1时才会发生。我已经更新了我的帖子以包含更多代码。我认为实际的问题可能是设置$selected var,因为它显示数组或加载很长一段时间。我以前应该问更多的情况。我认为您在这里误用了递归。我最初也希望这样,但我的代码无法知道行是什么。现在,一行通过CSS被截断,因为容器限制了每行20个座位的宽度。
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)