Home > Backend Development > PHP Tutorial > php取数组随机数

php取数组随机数

WBOY
Release: 2016-06-23 13:20:31
Original
1236 people have browsed it

$a=Array ( [0] => Array ( [0] => 8 [face_amount] => 8 ) [1] => Array ( [0] => 30 [face_amount] => 30 ) [2] => Array ( [0] => 50 [face_amount] => 50 ) [3] => Array ( [0] => 50 [face_amount] => 50 ) );//从数据库里取出的数据

//二维数组取一个值

$b=(array_rand($a,1)); 

echo $a[$b]['face_amount'];

//二维数组取多个值

$c=(array_rand($a,2)); 

echo $a[$c[0]]['face_amount'];

echo $a[$c[1]]['face_amount'];

//定义一个二维数组

$a=array(

  array("Volvo",22,18),

  array("BMW",25,13),

  array("Saab",5,2),

  array("Land Rover",17,15)

  );

//获取一个

$b=(array_rand($a,1));

var_dump($a[$b][0]);

//获取多个

$b=(array_rand($a,2));

var_dump($a[$b[0]][0]);

var_dump($a[$b[1]][1]);


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template