代码之家  ›  专栏  ›  技术社区  ›  Stark

如果数组的所有值都被检查过,如何抛出错误?

php
  •  1
  • Stark  · 技术社区  · 7 年前

    我写了一个小函数,它有一个数组,里面有一个值列表,我想调用这个函数,传递一个$AUTION参数,使它从数组中返回一个特定的信息量,所有的数据都是随机的和唯一的。

    我一直在检查错误,如果我们已经烧掉了所有可用的数组列表,我想抛出一个错误。

    function generate_array(int $amount){
        $array = array(0 => array(1), 1 => array(2), 2 => array(3), 3 => array(4));
        $count = 1;
        $arr = array();
        $tested = array();
    
        while($count <= $amount){
            $value = $array[array_rand($array)][0];
    
            /**
            * Error checking required
            */
            if(!in_array($value, $tested)) array_push($tested, $value);
    
            // here I need to check if all the values from $array has already been inserted in $tested or checked each one already
            if(count($tested) === $array) throw new \exception('error');    
    
            /** End of error checks */
    
            if(in_array($value, $arr)){
               continue; 
            } else {
                array_push($arr, $value);
                $count++;
            }
        }
    
        return $arr;
    }
    

    如果我们跑 print_r(generate_array(4)) 我们将得到一个带有4个键的数组,但是如果我们用一个参数值5而不是4来运行它,循环就没完没了地运行,这就是错误检查问题出现的地方,我想不出有什么办法来实现它。

    我已经包括了我试图添加的错误,但它不起作用。

    http://sandbox.onlinephpfunctions.com/code/3c00657740f7b9e88d0516ad1e5c7ee42807d213

    5 回复  |  直到 7 年前
        1
  •  1
  •   Lajos Arpad    7 年前

    这个

    if(!in_array($value, $tested)) array_push($tested, $value);
    

    只会 push $value 进入之内 $tested 如果它不存在于它里面。

    这个

    if(count($tested) === $array) throw new \exception('error');
    

    只会 throw 这个 Exception 如果 $测试 比赛 $array 是的。因此,你比较苹果和橘子,当所有的物品都用完了,至少还有另外一个元素需要使用,那么你将处于恼人的无限循环中。因此,您需要比较 count 元素:

    if(count($tested) === count($array)) throw new \Exception('error');
    

    你需要在循环块开始时抛出这个。但是由于您从一开始就知道数组中有多少元素,所以可以在循环和 不符合条件的例外情况:

    if ($amount > count($array)) throw new Exception('error');
    

    看看你密码里的案子, 例外情况 是一个大e,这只对你有效,因为你使用的是windows。如果在区分大小写的环境中运行此代码,则代码将崩溃。

        2
  •  1
  •   Philipp    7 年前

    为什么不在循环之前添加一个错误检查呢?

    if (count($array) < $amount) {
        return false;
    }
    
    while ($count <= $amount) ...
    
        3
  •  0
  •   TheFrack    7 年前

    你可以做一百万件事,但如果你想在while()循环中做,只需在末尾添加:

    if($count == $amount)
    {
    throw new \exception('error'); //If you want an error thrown
    break; //If you want to exit the loop after the "last go"
    }
    

    或者,如果要在循环开始时执行错误检查,请将条件更改为以下内容:

    if($amount > $count)
    {
    throw new \exception('error'); //If you want an error thrown
    break; //If you want to exit the loop
    }
    
        4
  •  0
  •   Nigel Ren    7 年前

    根据生成的数组是否可以稍微更改,可以简化整个代码…

    function generate_array(int $amount){
        $array = array(0 => 1, 1 => 2, 2 => 3, 3 => 4);
        shuffle($array);
        return ($amount <= count($array))?array_slice($array, 0, $amount):false;
    }
    

    这使用 shuffle() 把数组混合起来然后 array_slice() 获取所需数量的元素。

    它使用 $amount <= count($array) 检查物品的数量,但你可能会错过 数组切片() 只返回源数组中的项数

        5
  •  0
  •   pschichtel    7 年前

    你为什么要用循环开始呢?您可以检查被叫方是否请求的数据超过了可用的数据量( $amount > count($array) )抛出你的错误然后 shuffle() 数组返回一次 0 $amount - 1 从阵列中。这样,您只洗牌一次数据数组,不会有任何无休止的循环。