<?php
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/shared-manual.inc';
$TOC = array();
$TOC_DEPRECATED = array();
$PARENTS = array();
include_once dirname(__FILE__) ."/toc/ref.array.inc";
$setup = array (
  'home' => 
  array (
    0 => 'index.php',
    1 => 'PHP Manual',
  ),
  'head' => 
  array (
    0 => 'UTF-8',
    1 => 'zh',
  ),
  'this' => 
  array (
    0 => 'function.shuffle.php',
    1 => 'shuffle',
    2 => '打乱数组',
  ),
  'up' => 
  array (
    0 => 'ref.array.php',
    1 => '数组 函数',
  ),
  'prev' => 
  array (
    0 => 'function.rsort.php',
    1 => 'rsort',
  ),
  'next' => 
  array (
    0 => 'function.sizeof.php',
    1 => 'sizeof',
  ),
  'alternatives' => 
  array (
  ),
  'source' => 
  array (
    'lang' => 'zh',
    'path' => 'reference/array/functions/shuffle.xml',
  ),
  'history' => 
  array (
  ),
);
$setup["toc"] = $TOC;
$setup["toc_deprecated"] = $TOC_DEPRECATED;
$setup["parents"] = $PARENTS;
manual_setup($setup);

contributors($setup);

?>
<div id="function.shuffle" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">shuffle</h1>
  <p class="verinfo">(PHP 4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">shuffle</span> &mdash; <span class="dc-title">打乱数组</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.shuffle-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>shuffle</strong></span>(<span class="methodparam"><span class="type"><a href="language.types.array.php" class="type array">array</a></span> <code class="parameter reference">&$array</code></span>): <span class="type"><a href="language.types.singleton.php" class="type true">true</a></span></div>

  <p class="para rdfs-comment">
   本函数打乱（随机排列单元的顺序）一个数组。
  </p>
  <div class="caution"><strong class="caution">警告</strong>
 <p class="para">
  本函数并不会生成安全加密的值，并且<em>不可</em>用于加密或者要求返回值不可猜测的目的。
 </p>
 <p class="para">
  如果需要加密安全随机，则可以将 <span class="classname"><a href="class.random-engine-secure.php" class="classname">Random\Engine\Secure</a></span> 引擎用于
  <span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span>。对于简单的用例，<span class="function"><a href="function.random-int.php" class="function">random_int()</a></span>
  和 <span class="function"><a href="function.random-bytes.php" class="function">random_bytes()</a></span> 函数提供了操作系统的 <abbr title="Cryptographically Secure PseudoRandom Number Generator">CSPRNG</abbr> 支持的方便且安全的 <abbr title="Application Programming Interface">API</abbr>。
 </p>
</div>
  <div class="caution"><strong class="caution">警告</strong>
 <p class="simpara">
    此函数使用全局 Mt19937（“梅森旋转算法”）实例作为随机源，因此与所有其他使用全局 Mt19937 的函数共享其状态。
    使用这些函数中的任何一个都会推进<em>所有</em>其他函数的序列，无论作用域如何。
  </p>
  <p class="simpara">
    通过向 <span class="function"><a href="function.mt-srand.php" class="function">mt_srand()</a></span> 或 <span class="function"><a href="function.srand.php" class="function">srand()</a></span> 以已知值播种来生成可重复的序列也将从此函数产生可重复的输出。
  </p>
  <p class="simpara">
    在所有新编写的代码中，建议使用 <span class="classname"><a href="class.random-randomizer.php" class="classname">Random\Randomizer</a></span> 的方法。
 </p>
</div>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.shuffle-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt><code class="parameter">array</code></dt>
     <dd>
      <p class="para">
       待操作的数组。
      </p>
     </dd>
    
   </dl>
  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.shuffle-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   总是返回 <strong><code><a href="reserved.constants.php#constant.true">true</a></code></strong>。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.shuffle-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>7.1.0</td>
       <td>
        内置的随机数产生算法从 libc rand 函数<a href="migration71.incompatible.php#migration71.incompatible.rand-srand-aliases" class="link">改成</a><a href="http://www.math.sci.hiroshima-u.ac.jp/~m-mat/MT/emt.html" class="link external">&raquo;&nbsp;梅森旋转</a>伪随机数生成算法。
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 
 <div class="refsect1 examples" id="refsect1-function.shuffle-examples">
  <h3 class="title">示例</h3>
  <p class="para">
   <div class="example" id="example-1">
    <p><strong>示例 #1 <span class="function"><strong>shuffle()</strong></span> 例子</strong></p>
    <div class="example-contents">
<div class="annotation-interactive phpcode"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php<br />$numbers </span><span style="color: #007700">= </span><span style="color: #0000BB">range</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">20</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">shuffle</span><span style="color: #007700">(</span><span style="color: #0000BB">$numbers</span><span style="color: #007700">);<br />foreach (</span><span style="color: #0000BB">$numbers </span><span style="color: #007700">as </span><span style="color: #0000BB">$number</span><span style="color: #007700">) {<br />    echo </span><span style="color: #DD0000">"</span><span style="color: #0000BB">$number</span><span style="color: #DD0000"> "</span><span style="color: #007700">;<br />}<br /></span><span style="color: #0000BB">?&gt;</span></span></code></div>
    </div>

   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.shuffle-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">注意</strong>: <span class="simpara">此函数为 <code class="parameter">array</code>
中的元素赋与新的键名。这将删除原有的键名，而不是仅仅将键名重新排序。</span></p></blockquote>
  <blockquote class="note"><p><strong class="note">注意</strong>: 
 <p class="para">
    重置数组中的内部指针，指向第一个元素。
 </p>
</p></blockquote>

 </div>

 <div class="refsect1 seealso" id="refsect1-function.shuffle-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li><span class="function"><a href="random-randomizer.shufflearray.php" class="function" rel="rdfs-seeAlso">Random\Randomizer::shuffleArray()</a> - Get a permutation of an array</span></li>
    <li><span class="function"><a href="random-randomizer.shufflebytes.php" class="function" rel="rdfs-seeAlso">Random\Randomizer::shuffleBytes()</a> - Get a byte-wise permutation of a string</span></li>
    <li><span class="function"><a href="random-randomizer.pickarraykeys.php" class="function" rel="rdfs-seeAlso">Random\Randomizer::pickArrayKeys()</a> - Select random array keys</span></li>
    <li><a href="array.sorting.php" class="link">数组排序函数对比</a></li>
   </ul>
  </p>
 </div>

  </div><?php manual_footer($setup); ?>