开发者

php shuffle() returns one item

开发者 https://www.devze.com 2022-12-25 22:38 出处:网络
Anyone have any idea why shuffle() would only return 1 item? when using: $array2 = shuffle($array1); with the following array($array1):

Anyone have any idea why shuffle() would only return 1 item?

when using:

$array2 = shuffle($array1);

with the following array($array1):

Array
(
    [0] => 1
    [1] => 5
    [2] => 6
    [3] => 7
    [4] =&开发者_开发技巧gt; 8
    [5] => 10
    [6] => 11
    [7] => 12
    [8] => 13
    [9] => 14
)

The output of:

print_r($array2);

is simply: 1

Any idea as to why it would not only not shuffle the array, but knock off the remaining 9 items in the array?

thanks!


shuffle() shuffles the array in place, and returns true if it succeeded. If you want $array2 to be a shuffled version of $array1, first make it a copy of $array1 and then call shuffle($array2);

See the docs: shuffle


shuffle changes the original array. So in your case the shuffled array is $array1.

$array2 is simply a boolean value. The function returns true or false.


Please read a function description before use http://php.net/shuffle it may work other than you expect.


$array2 = $array1;
shuffle($array2);
print_r($array2);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号