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

WBOY
Lepaskan: 2016-08-20 09:04:15
asal
970 orang telah melayarinya

先上代码

<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>
Salin selepas log masuk
Salin selepas log masuk

我想从$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>
Salin selepas log masuk
Salin selepas log masuk

我想从$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>
Salin selepas log masuk

不论运行多少次,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>
Salin selepas log masuk

结果为空:

<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>
Salin selepas log masuk

结果不为空:

<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>
Salin selepas log masuk
Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!