Randomly remove several different numbers from an array
/* * 此程序是从一个不重复的数组中随机的取出若干个不同的元素 * 难点是防止在取数的时候出现已经取到过的情况(特别是取到最后),需要尽可能的降低碰撞 */ //第一种算法,CSDN上别人的想法 /* $num = 0; $array = array(1, 2, 3, 4, 5, 6, 7, 8, 9); $arr = array(); $g = 5; $tag = true; while ($tag) { $count = count($array); $t = rand(0, 1); if ($t == 1) { $arr[] = $array[$num]; unset($array[$num]); } $num ++; if (count($arr) == $g) { $tag = false; } if ($num == $count) { $num = 0; //循环 } } var_dump($arr); */ //第二种算法,自己想的。 //可以在每次取出数据之后将该数据和最后没有获取的数据替换,然后再去没有取得的数据中随机获取值 function swap(&$a, &$b) { $temp = $b; $b = $a; $a = $temp; } $result = array(); $src = array(); for($i = 0 ; $i < 40 ; $i++) { $src[] = $i + 1; } $arr_len = count($src); $count = 20; $index = 0; while($index < $count) { $random = rand(0, $arr_len - $index - 1); $result[] = $src[$random]; swap($src[$random] , $src[$arr_len - $index - 1]); $index += 1; } print_r(json_encode($result)); print_r(json_encode($src));
Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.
The above introduces how to randomly extract several different numbers from an array, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











index.html represents the home page file of the web page and is the default page of the website. When a user visits a website, the index.html page is usually loaded first. HTML (HypertextMarkupLanguage) is a markup language used to create web pages, and index.html is also an HTML file. It contains the structure and content of a web page, as well as tags and elements used for formatting and layout. Here is an example index.html code: <

src and href are respectively, 1. src is the abbreviation of source, which is used to specify the path of external resources. It is usually used to embed external files, such as pictures, audios, videos, etc. The src attribute is generally used on img, script, iframe and other tags. ; 2. href is the abbreviation of hypertext reference, which is used to specify the path of the target resource of the hyperlink. It is usually used to link to external documents or other pages. The href attribute is generally used on tags such as a and link.

The Count function is used to count the number of numbers in a specified range. It ignores text, logical values, and null values, but counts empty cells. The Count function only counts the number of cells that contain actual numbers. The CountA function is used to count the number of non-empty cells in a specified range. It not only counts cells containing actual numbers, but also counts the number of non-empty cells containing text, logical values, and formulas.

Title: Example of using the Array.Sort function to sort an array in C# Text: In C#, array is a commonly used data structure, and it is often necessary to sort the array. C# provides the Array class, which has the Sort method to conveniently sort arrays. This article will demonstrate how to use the Array.Sort function in C# to sort an array and provide specific code examples. First, we need to understand the basic usage of the Array.Sort function. Array.So

When programming in PHP, we often need to merge arrays. PHP provides the array_merge() function to complete array merging, but when the same key exists in the array, this function will overwrite the original value. In order to solve this problem, PHP also provides an array_merge_recursive() function in the language, which can merge arrays and retain the values of the same keys, making the program design more flexible. array_merge

In PHP, there are many powerful array functions that can make array operations more convenient and faster. When we need to combine two arrays into an associative array, we can use PHP's array_combine function to achieve this operation. This function is actually used to combine the keys of one array as the values of another array into a new associative array. Next, we will explain how to use the array_combine function in PHP to combine two arrays into an associative array. Learn about array_comb

Get requests sent by href and src. Detailed description: 1. The href attribute is used to specify the target resource of the link. When referencing an external style sheet, it will send a GET request to obtain the CSS file. When referencing the document, it will send a GET request to obtain the specified HTML file. When referencing an image, it will send a GET request. To obtain the specified image file; 2. The src attribute is used to specify the URL of the embedded resource. When referencing the image, it will send a GET request to obtain the specified image file. When referencing the audio, it will send a GET request to obtain the specified audio file, etc. .

PHP source code running problem: Index error resolution requires specific code examples. PHP is a widely used server-side scripting language that is often used to develop dynamic websites and web applications. However, sometimes you will encounter various problems when running PHP source code, among which "index error" is a common situation. This article will introduce some common causes and solutions of index errors, and provide specific code examples to help readers better deal with such problems. Problem Description: When running a PHP program
