2010-03-28 22:10:10 +00:00
<?xml version="1.0" encoding="utf-8"?>
2009-07-11 06:30:45 +00:00
<!-- $Revision$ -->
2017-02-20 22:01:10 +00:00
<refentry xml:id= "function.array-rand" xmlns= "http://docbook.org/ns/docbook" xmlns:xlink= "http://www.w3.org/1999/xlink" >
2006-10-31 11:24:02 +00:00
<refnamediv >
<refname > array_rand</refname>
2018-05-08 12:29:26 +00:00
<refpurpose > Pick one or more random keys out of an array</refpurpose>
2006-10-31 11:24:02 +00:00
</refnamediv>
2007-12-30 11:35:36 +00:00
<refsect1 role= "description" >
&reftitle.description;
2006-10-31 11:24:02 +00:00
<methodsynopsis >
2020-12-04 00:35:15 +00:00
<type class= "union" > <type > int</type> <type > string</type> <type > array</type> </type> <methodname > array_rand</methodname>
2013-09-24 13:06:51 +00:00
<methodparam > <type > array</type> <parameter > array</parameter> </methodparam>
<methodparam choice= "opt" > <type > int</type> <parameter > num</parameter> <initializer > 1</initializer> </methodparam>
2006-10-31 11:24:02 +00:00
</methodsynopsis>
<para >
2010-10-28 14:52:00 +00:00
Picks one or more random entries out of an array, and returns the
2010-08-10 01:31:30 +00:00
key (or keys) of the random entries.
2016-06-14 14:52:22 +00:00
It uses a pseudo random number generator that is not suitable for
cryptographic purposes.
2006-10-31 11:24:02 +00:00
</para>
2007-12-30 11:35:36 +00:00
</refsect1>
<refsect1 role= "parameters" >
&reftitle.parameters;
<para >
<variablelist >
<varlistentry >
2013-09-24 13:06:51 +00:00
<term > <parameter > array</parameter> </term>
2007-12-30 11:35:36 +00:00
<listitem >
<para >
The input array.
</para>
</listitem>
</varlistentry>
<varlistentry >
2013-09-24 13:06:51 +00:00
<term > <parameter > num</parameter> </term>
2007-12-30 11:35:36 +00:00
<listitem >
<para >
2013-11-18 18:11:43 +00:00
Specifies how many entries should be picked.
2007-12-30 11:35:36 +00:00
</para>
</listitem>
</varlistentry>
</variablelist>
</para>
</refsect1>
<refsect1 role= "returnvalues" >
&reftitle.returnvalues;
2006-10-31 11:24:02 +00:00
<para >
2013-11-18 18:11:43 +00:00
When picking only one entry, <function > array_rand</function> returns
the key for a random entry. Otherwise, an array of keys for the random
entries is returned. This is done so that random keys can be picked
2020-10-19 07:35:23 +00:00
from the array as well as random values. If multiple keys are returned,
they will be returned in the order they were present in the original array.
Trying to pick more elements
2013-11-18 18:11:43 +00:00
than there are in the array will result in an
<constant > E_WARNING</constant> level error, and NULL will be returned.
2006-10-31 11:24:02 +00:00
</para>
2007-12-30 11:35:36 +00:00
</refsect1>
2009-05-10 17:55:00 +00:00
<refsect1 role= "changelog" >
&reftitle.changelog;
<para >
<informaltable >
<tgroup cols= "2" >
<thead >
<row >
<entry > &Version; </entry>
<entry > &Description; </entry>
</row>
</thead>
2017-02-20 22:01:10 +00:00
<tbody >
<row >
<entry > 7.1.0</entry>
<entry >
2018-04-17 16:10:01 +00:00
The internal randomization algorithm <link linkend= "migration71.incompatible.rand-srand-aliases" > has been changed</link> to use the <link xlink:href= "&url.mersenne;" > Mersenne Twister</link> Random Number Generator instead of the libc rand function.
2017-02-20 22:01:10 +00:00
</entry>
</row>
2009-05-10 17:55:00 +00:00
</tbody>
</tgroup>
</informaltable>
</para>
</refsect1>
2007-12-30 11:35:36 +00:00
<refsect1 role= "examples" >
&reftitle.examples;
2006-10-31 11:24:02 +00:00
<para >
<example >
<title > <function > array_rand</function> example</title>
<programlisting role= "php" >
2002-04-15 00:12:54 +00:00
< ![CDATA[
2003-05-30 17:44:31 +00:00
< ?php
2003-08-17 12:21:03 +00:00
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
2003-12-15 16:55:22 +00:00
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
2003-05-30 17:44:31 +00:00
?>
2002-04-15 00:12:54 +00:00
]]>
2006-10-31 11:24:02 +00:00
</programlisting>
</example>
</para>
2007-12-30 11:35:36 +00:00
</refsect1>
<refsect1 role= "seealso" >
&reftitle.seealso;
2006-10-31 11:24:02 +00:00
<para >
2007-12-30 11:35:36 +00:00
<simplelist >
<member > <function > shuffle</function> </member>
</simplelist>
2006-10-31 11:24:02 +00:00
</para>
</refsect1>
</refentry>
2002-04-15 00:12:54 +00:00
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
2009-09-25 07:04:39 +00:00
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
2002-04-15 00:12:54 +00:00
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->