Home > Backend Development > PHP Tutorial > php空数组随机安插

php空数组随机安插

WBOY
Release: 2016-06-13 11:13:19
Original
1245 people have browsed it

php空数组随机插入

本帖最后由 mahuatengBC 于 2012-12-17 12:50:13 编辑 如题

假设现在有一个1000个元素空数组,现在要随机插入5个一等奖,随机插入10个二等奖,随机插入20个三等奖,其余的都是未中奖

也就是说,这一千个元素中必须有5个一等奖 10个二等奖 20个三等奖 其他未中奖

该如何实现呢?


------解决方案--------------------
$ar = array_merge(array_fill(0, 5, 1), array_fill(0, 10, 2), array_fill(0, 20, 3), array_fill(0, 965, 0));
shuffle($ar);

$ar 即是你需要的数组
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