Heim  >  Artikel  >  Backend-Entwicklung  >  为什么这段代码执行完成后,会有数组为空的情况?

为什么这段代码执行完成后,会有数组为空的情况?

WBOY
WBOYOriginal
2016-08-20 09:04:15966Durchsuche

先上代码

<code>$suffix = 0; //用户ID尾数
$ids = [];
$stores = [1,10,23,45,67,56,45,324,23]; //储存用户ID的数组
$limit = 5;
$count = count($stores);
$i=0;
do{
    $id = $stores[array_rand($stores ,1)];
    $sub = substr($id ,-1);
    if($sub==$suffix){
        $ids[$id] = $id;
    }
    $i++;
}while(count($ids)</code>

我想从$stores中获取尾数为0的用户ID,最多取5个,最少取1个,结果存到数组中,就有了上面的代码
,但是我执行它,竟然会有$ids为空的时候。。。搞不懂。。。

回复内容:

先上代码

<code>$suffix = 0; //用户ID尾数
$ids = [];
$stores = [1,10,23,45,67,56,45,324,23]; //储存用户ID的数组
$limit = 5;
$count = count($stores);
$i=0;
do{
    $id = $stores[array_rand($stores ,1)];
    $sub = substr($id ,-1);
    if($sub==$suffix){
        $ids[$id] = $id;
    }
    $i++;
}while(count($ids)</code>

我想从$stores中获取尾数为0的用户ID,最多取5个,最少取1个,结果存到数组中,就有了上面的代码
,但是我执行它,竟然会有$ids为空的时候。。。搞不懂。。。

为什么要用while而且循环到的数据还是随机取数组的某个元素,不能使用foreach吗?

结果不为空的话试试这个

<code>        $suffix = 0; //用户ID尾数
        $ids = [];
        $stores = [1,10,23,45,67,56,45,324,23]; //储存用户ID的数组
        $limit = 5;
        $count = count($stores);
        $i=0;
        do{
            $id = $stores[array_rand($stores ,1)];
            $sub = substr($id ,-1);
            if($sub==$suffix){
                $ids[$id] = $id;
            }
            $i++;
            $iCount = count($ids);
        }while($iCount == 0 || ($count</code>

不论运行多少次,array_rand($stores ,1) ,都有可能有数组元素永远取不到。
$stores尾数是0的就只有10;
$count = count($stores);意味着最多do_while 10次。

10次都没轮上那个10的话,最后就只能得到空数组了。

<code><?php $suffix = 0; //用户ID尾数
$ids = [];
$stores = [1,10,23,45,67,56,45,324,23]; //储存用户ID的数组
$limit = 5;
$count = count($stores);
$i = 0;
do {
    $id = $stores[array_rand($stores, 1)];
    $sub = substr($id, -1);
    echo 'test:'.$id."\n";
    if ($sub == $suffix) {
        $ids[$id] = $id;
        echo 'got:'.$id."\n";
    }
    ++$i;
    echo 'count($ids):'.count($ids)."\n";
    echo '$i:'.$i."\n";

} while (count($ids) < $limit && $i < $count);
print_r($ids);</code></code>

结果为空:

<code>[root@localhost www]# php test.php
test:1
count($ids):0
$i:1
test:23
count($ids):0
$i:2
test:23
count($ids):0
$i:3
test:56
count($ids):0
$i:4
test:45
count($ids):0
$i:5
test:67
count($ids):0
$i:6
test:324
count($ids):0
$i:7
test:67
count($ids):0
$i:8
test:67
count($ids):0
$i:9
Array
(
)</code>

结果不为空:

<code>[root@localhost www]# php test.php
test:324
count($ids):0
$i:1
test:10
got:10
count($ids):1
$i:2
test:23
count($ids):1
$i:3
test:10
got:10
count($ids):1
$i:4
test:67
count($ids):1
$i:5
test:324
count($ids):1
$i:6
test:45
count($ids):1
$i:7
test:45
count($ids):1
$i:8
test:1
count($ids):1
$i:9
Array
(
    [10] => 10
)</code>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn