This article will introduce to you some commonly used PHP example programs for obtaining random arrays in arrays. I hope this method will be helpful to all my friends.
Post a piece of code to get a random array in php. Needless to say, just post the code directly. array_rand in php is very abnormal, breaking through the understanding of normal people, and is cumbersome
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;
}
|
| Copy code
|
代码如下 |
复制代码 |
$a = array(0,1,2,3,4,5,6,7,8);
echo "$a原来的顺序为: ";
foreach($a as $v)
echo $v."t";
shuffle($a);
echo " $a被打乱后的顺序为: ";
foreach($a as $v)
echo $v."t";
?>
|
function create_random_ids( $min,$max,$limited )
{
$_base_ids = range($min,$max);
$_temp_key = array_rand ($_base_ids,min(count($_base_ids),$limited+10));
//Splicing
$ids = array();<img style="max-width:90%" alt="php 随机打乱一个数组的排序shuffle - 九重海 - jiuchonghai-PHP的博客" src="http://www.bkjia.com/uploads/allimg/131231/132F0A18-0.jpg" __1379995076531__="ev_3284125348"> Copy after login
for ($x=0; $x < count($_temp_key); $x++) {
$ids[] = $_base_ids[$_temp_key[$x]];<strong>第二次得到的结果为: </strong> Copy after login
}<img style="max-width:90%" alt="php 随机打乱一个数组的排序shuffle - 九重海 - jiuchonghai-PHP的博客" src="http://www.bkjia.com/uploads/allimg/131231/132F04U9-1.jpg" __1379995076531__="ev_5189766409"> Copy after login
Return $ids;<strong>第三次得到的结果为:<br></strong> <img style="max-width:90%" alt="php 随机打乱一个数组的排序shuffle - 九重海 - jiuchonghai-PHP的博客" src="http://www.bkjia.com/uploads/allimg/131231/132F03115-2.jpg" __1379995076531__="ev_6379571093"> Copy after login
} |
Example 2
The code is as follows
|
Copy code
$a = array(0,1,2,3,4,5,6,7,8);
echo "The original order of $a is: ";
foreach($a as $v)
echo $v."t";
shuffle($a);
echo " The scrambled order of $a is: ";
foreach($a as $v)
echo $v."t";
?>
The first result is:
http://www.bkjia.com/PHPjc/631246.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631246.htmlTechArticleThis article will introduce you to some commonly used PHP example programs for obtaining random arrays in arrays. I hope this method will be useful for Friends, this is helpful. Post a piece of code to get random...
|
|