Home >Backend Development >PHP Tutorial >PHP method example code to obtain a random array list

PHP method example code to obtain a random array list

怪我咯
怪我咯Original
2017-07-09 09:22:531121browse

This article mainly introduces the method of php to obtain a random array list, involving the application of random numbers to array traversal, which is of great practical value. Friends who need it can refer to it.

The example in this article describes the example program of obtaining a random array in an array in PHP, and shares it with you for your reference. The specific implementation method is as follows:

Needless to say, just paste the code directly. array_rand in php is very abnormal, breaks through the understanding of normal people, and is very complicated
Example 1:

The code is as follows:

function create_random_ids( $min,$max,$limited )
{
    $_base_ids = range($min,$max);
    $_temp_key = array_rand ($_base_ids,min(count($_base_ids),$limited+10));
    //拼接
    $ids = array();
    for ($x=0; $x < count($_temp_key); $x++) {
        $ids[] = $_base_ids[$_temp_key[$x]];
    }
    return $ids;
}

Example 2:

The code is as follows:

<?php 
$a = array(0,1,2,3,4,5,6,7,8);
echo "$a原来的顺序为:<hr/>";
foreach($a as $v)
 echo $v."t";
shuffle($a);
echo "<br/>$a被打乱后的顺序为:<hr/>";
foreach($a as $v)
 echo $v."t";
?>


The first result is:

The result obtained for the second time is:

The result obtained for the third time is:

The above is the detailed content of PHP method example code to obtain a random array list. For more information, please follow other related articles on the PHP Chinese website!

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