当我需要检查array2是否有来自array1的某个值(随机生成)时,我遇到了这种情况。到目前为止,我虽然是
redo : $id=mt_rand(0,count(array1));
foreach($array2 as $arr)
{
if($arr[0]==$id) goto redo;
}
//Some actions if randomly generated value from array1 wasn't found in array2
但我真的不想使用goto。我很确定有一些简单的解决方案可以在没有goto的情况下做到这一点,但我无法想到它D:
答案 0 :(得分:1)
您可以将数字参数与continue
一起使用:
http://www.php.net/manual/en/control-structures.continue.php
while(true){
$id = mt_rand(0,count(array1);
foreach( $array2 as $arr )
// restart the outer while loop if $id found
if( $arr[0] == $id ) continue 2;
// $id not found in array, leave the while loop ...
break;
};
// ... and do the action
答案 1 :(得分:1)
试试这个
$flag=true;
do{
$id=mt_rand(0,count(array1);
foreach( $array2 as $arr )
if( $arr[0] == $id ) break;
// do it and set flag to false when you need to exit;
} while($flag);