>  기사  >  백엔드 개발  >  이 코드가 실행된 후 배열이 비어 있는 이유는 무엇입니까?

이 코드가 실행된 후 배열이 비어 있는 이유는 무엇입니까?

WBOY
WBOY원래의
2016-08-20 09:04:15968검색

먼저 코드를 받으세요

<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);는 최대 10번의 do_while을 의미합니다.

숫자 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>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.