為什麼這段程式碼執行完成後,會有陣列為空的情況?

WBOY
發布: 2016-08-20 09:04:15
原創
970 人瀏覽過

先上程式碼

<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)<$limit && $i<$count);
print_r($ids);
exit;</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)<$limit && $i<$count);
print_r($ids);
exit;</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<$limit && $i<$count ));
        print_r($ids);
        exit;</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>[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>
登入後複製
相關標籤:
php
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!