Home Backend Development PHP Tutorial 遇到怪事了 PHP 设定了range范围 但运行结果却是显示在范围之外,为什么

遇到怪事了 PHP 设定了range范围 但运行结果却是显示在范围之外,为什么

Jun 13, 2016 pm 12:26 PM
array nbsp range red

碰到怪事了 PHP 设定了range范围 但运行结果却是显示在范围之外,为什么?
一段PHP随机代码,设定了range(1,30)但多次运行结果会显示出0,why?
代码:
$red1=range(1,30);
$sc1=array_rand($red1,8);
?>


foreach($sc1 as $v){
echo "$v ";
}
?>

$red2=range(1,30);
$sc1=array_rand($red2,8);
?>

foreach($sc1 as $v){
echo "$v ";
}
?>
$red1=range(1,30);
$sc1=array_rand($red1,8);
?>

foreach($sc1 as $v){
echo "$v ";
}
?>

$red2=range(1,30);
$sc1=array_rand($red2,8);
?>

foreach($sc1 as $v){
echo "$v ";
}
?>
请多刷新几次,第一个数竟然会出现0?。。为什么?


------解决思路----------------------

------解决思路----------------------
你想要的应该是打乱数组排序,取8个元素出来是吧?

shuffle($red1);
$red1 = array_splice($red1,0,8);
------解决思路----------------------
array_rand()随机取出来的是键名;
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

10 Ways to Adjust Brightness on Windows 11 10 Ways to Adjust Brightness on Windows 11 Dec 18, 2023 pm 02:21 PM

Screen brightness is an integral part of using modern computing devices, especially when you look at the screen for long periods of time. It helps you reduce eye strain, improve legibility, and view content easily and efficiently. However, depending on your settings, it can sometimes be difficult to manage brightness, especially on Windows 11 with the new UI changes. If you're having trouble adjusting brightness, here are all the ways to manage brightness on Windows 11. How to Change Brightness on Windows 11 [10 Ways Explained] Single monitor users can use the following methods to adjust brightness on Windows 11. This includes desktop systems using a single monitor as well as laptops. let's start. Method 1: Use the Action Center The Action Center is accessible

FiiO CP13 cassette player launches with transparent retro look FiiO CP13 cassette player launches with transparent retro look Jun 16, 2024 am 09:52 AM

TheFiiOCP13cassetteplayerwasannouncedinJanuary.Now,FiiOisexpandingitsportfoliowithtwonewmodels-onewitharedfrontandonewithatransparentfront.Thelatternotonlyperfectlymatchestheretrocharmoftheangulardesign,butalso

What are the blockchain data analysis tools? What are the blockchain data analysis tools? Feb 21, 2025 pm 10:24 PM

The rapid development of blockchain technology has brought about the need for reliable and efficient analytical tools. These tools are essential to extract valuable insights from blockchain transactions in order to better understand and capitalize on their potential. This article will explore some of the leading blockchain data analysis tools on the market, including their capabilities, advantages and limitations. By understanding these tools, users can gain the necessary insights to maximize the possibilities of blockchain technology.

Binance Launchpool No. 64 project analysis and price forecast: Can RedStone (RED) ignite the market? Binance Launchpool No. 64 project analysis and price forecast: Can RedStone (RED) ignite the market? Mar 03, 2025 pm 11:45 PM

Hello everyone, I am your old friend, a friend who talks to you about the crypto market in Binance Plaza all year round. Binance Launchpool recently launched the 64th phase project - RedStone (RED). As a multi-chain oracle project, it sparked a lot of discussion before it was launched. Today we will have an in-depth analysis of RED to see how its potential and how the price may go after it is launched. Binance Launchpool No. 64 project analysis and price forecast: Can RedStone (RED) ignite the market? Project background and core highlights RedStone (RED) is a multi-chain oracle platform focusing on decentralized finance (DeFi), with the goal of E

PHP returns all the values ​​in the array to form an array PHP returns all the values ​​in the array to form an array Mar 21, 2024 am 09:06 AM

This article will explain in detail how PHP returns all the values ​​of an array to form an array. The editor thinks it is quite practical, so I share it with you as a reference. I hope you can gain something after reading this article. Using the array_values() function The array_values() function returns an array of all the values ​​in an array. It does not preserve the keys of the original array. $array=["foo"=>"bar","baz"=>"qux"];$values=array_values($array);//$values ​​will be ["bar","qux"]Using a loop can Use a loop to manually get all the values ​​of the array and add them to a new

The track is cold, can the AI ​​Agent in the vertical field break the deadlock? The track is cold, can the AI ​​Agent in the vertical field break the deadlock? Mar 05, 2025 am 07:57 AM

Web3 Vertical AIAgent: Subvert tradition and reshape the industry landscape? This paper discusses the application differences of AIAgent in Web2 and Web3 and the future potential of Web3Agent. Web2 has been widely used to improve efficiency, covering sales, marketing and other fields, and has achieved significant economic benefits. Web3Agent combines blockchain technology to open up new application scenarios, especially in the DeFi field. It demonstrates potential beyond Web2Agent through token incentives, decentralized platforms and on-chain data analysis. Although Web3Agent is currently facing challenges, its unique advantages make it expected to compete with Web2 in the medium and long term, and even reshape the industry landscape. Web2AI

Nikon officially announces completion of acquisition of movie camera company RED Nikon officially announces completion of acquisition of movie camera company RED Apr 13, 2024 pm 09:22 PM

According to news from this site on April 13, Nikon officially announced that it has completed the acquisition of RED, and RED has officially become a subsidiary of Nikon. According to the official announcement, RED’s former president Jarred Land and founder James Jannard will serve as consultants for the new company, and Nikon Imaging Division Keiji Oishi will be appointed as CEO of the new company. Tommy Rios, the former executive vice president of RED, will be promoted to co-CEO of the new company. The announcement stated that there will be no changes to RED’s current product lineup, partners, and relationships with dealers. RED will continue to support its policies through warranty, repair services, customer service and overall product support. Nikon said Nikon and RED will combine the strengths of both companies to develop unique products while leveraging

Vertical proxy: Application scenarios and interpretation of disruptive potential of encryption native proxy Vertical proxy: Application scenarios and interpretation of disruptive potential of encryption native proxy Mar 04, 2025 am 10:21 AM

Artificial intelligence agents (AIAgents) are rapidly integrating into daily operations of enterprises, from large companies to small businesses, almost all areas have begun to be used, including sales, marketing, finance, law, IT, project management, logistics, customer service and workflow automation. We are moving from an era of manual processing of data, performing repetitive tasks, and using Excel tables to an era of autonomous operation by AI agents around the clock, which not only improves efficiency but also significantly reduces costs. Application case of AI agents in Web2: YCombinator's Perspective Apten: A sales and marketing optimization tool combining AI and SMS technology. BildAI: A model that can read architectural blueprints,

See all articles