Home  >  Article  >  Backend Development  >  Why is the array empty after this code is executed?

Why is the array empty after this code is executed?

WBOY
WBOYOriginal
2016-08-20 09:04:15968browse

Get the code first

<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>

I want to get user IDs ending in 0 from $stores. The maximum number is 5 and the minimum number is 1. The result is stored in an array. I have the above code
, but when I execute it, there are $ids as When empty. . . Don't understand. . .

Reply content:

Get the code first

<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>

I want to get user IDs ending in 0 from $stores. The maximum number is 5 and the minimum number is 1. The result is stored in an array. I have the above code
, but when I execute it, there are $ids as When empty. . . Don't understand. . .

Why do you need to use while and the data looped through is still a random element of the array? Can't you use foreach?

If the result is not empty, try this

<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>

No matter how many times you run array_rand($stores,1), there may be array elements that can never be retrieved.
$stores with a mantissa of 0 is only 10;
$count = count($stores); means do_while at most 10 times.

If you don’t get the number 10 after 10 times, you will only get an empty array in the end.

<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>

The result is empty:

<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>

The result is not empty:

<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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn