Home > Backend Development > PHP Problem > How to randomly extract several values ​​from an array in php

How to randomly extract several values ​​from an array in php

青灯夜游
Release: 2023-03-10 21:26:01
Original
7162 people have browsed it

In PHP, you can use the array_rand() function to randomly take out several values ​​from the array. This function can randomly get one or more elements from the array and return the obtained array name; the syntax format is "array_rand (array,num)", the parameter num specifies the number of array elements to be obtained.

How to randomly extract several values ​​from an array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

In PHP, you can use the array_rand() function Randomly pick out several values ​​from the array.

array_rand() function can randomly obtain one or more elements from an array and return the obtained array key name. This function uses a pseudo-random number generation algorithm, so it is not suitable for cryptography scenarios. Its syntax format is as follows:

array_rand(array,number)
Copy after login
## Optional. Specifies how many random elements to return.
ParametersDescription
arrayRequired. Specifies an array.
number
Tip: If you omit the number parameter, the function will return a random array key name by default. If the number obtained exceeds the length of the array, it will Will cause an E_WARNING error and return NULL.

If only one element needs to be obtained, the array_rand() function will return the obtained key name, otherwise it will return an array containing random key names. After the function runs successfully, the specific value can be obtained based on the randomly obtained array key value.

Example: Use the array_rand() function to randomly obtain array elements

<?php
    $array = array(
        &#39;name&#39;  => &#39;php中文网&#39;,
        &#39;url&#39;   => &#39;http://www.php.cn&#39;,
        &#39;title&#39; => &#39;PHP教程&#39;,
        &#39;article&#39; => &#39;array_rand()函数&#39;
    );
    echo &#39;<pre class="brush:php;toolbar:false">&#39;;
    var_dump(array_rand($array));
    var_dump(array_rand($array, 2));
    var_dump(array_rand($array, 2));
    var_dump(array_rand($array, 5));
?>
Copy after login

How to randomly extract several values ​​from an array in php

Recommended learning: "

PHP Video Tutorial"

The above is the detailed content of How to randomly extract several values ​​from an array in php. For more information, please follow other related articles on the PHP Chinese website!

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