mirror of
https://github.com/fofolee/uTools-Manuals.git
synced 2025-06-18 13:57:03 +08:00
208 lines
8.5 KiB
HTML
208 lines
8.5 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
|
<html>
|
|
<head>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<title>移除数组中重复的值</title>
|
|
</head>
|
|
<body class="docs"><div id="layout">
|
|
<div id="layout-content"><div id="function.array-unique" class="refentry">
|
|
<div class="refnamediv">
|
|
<h1 class="refname">array_unique</h1>
|
|
<p class="verinfo">(PHP 4 >= 4.0.1, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">array_unique</span> — <span class="dc-title">移除数组中重复的值</span></p>
|
|
|
|
</div>
|
|
<div class="refsect1 description" id="refsect1-function.array-unique-description">
|
|
<h3 class="title">说明</h3>
|
|
<div class="methodsynopsis dc-description">
|
|
<span class="methodname"><strong>array_unique</strong></span>
|
|
( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array</code></span>
|
|
[, <span class="methodparam"><span class="type">int</span> <code class="parameter">$sort_flags</code><span class="initializer"> = SORT_STRING</span></span>
|
|
] ) : <span class="type">array</span></div>
|
|
|
|
<p class="para rdfs-comment">
|
|
<span class="function"><strong>array_unique()</strong></span> 接受
|
|
<code class="parameter">array</code> 作为输入并返回没有重复值的新数组。
|
|
</p>
|
|
<p class="para">
|
|
注意键名保留不变。<span class="function"><strong>array_unique()</strong></span>
|
|
先将值作为字符串排序,然后对每个值只保留第一个遇到的键名,接着忽略所有后面的键名。这并不意味着在未排序的
|
|
<code class="parameter">array</code> 中同一个值的第一个出现的键名会被保留。
|
|
</p>
|
|
<blockquote class="note"><p><strong class="note">Note</strong>:
|
|
<span class="simpara">
|
|
当且仅当
|
|
<em>(string) $elem1 === (string) $elem2</em> 时两个单元被认为相同。
|
|
例如,字符串表达一样时,会使用首个元素。
|
|
</span>
|
|
</p></blockquote>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 parameters" id="refsect1-function.array-unique-parameters">
|
|
<h3 class="title">参数</h3>
|
|
<p class="para">
|
|
<dl>
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">array</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
输入的数组。
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
|
|
<dt>
|
|
<code class="parameter">sort_flags</code></dt>
|
|
|
|
<dd>
|
|
|
|
<p class="para">
|
|
第二个可选参数<code class="parameter">sort_flags</code> 可用于修改排序行为:
|
|
</p>
|
|
<p class="para">
|
|
排序类型标记:
|
|
<ul class="itemizedlist">
|
|
<li class="listitem">
|
|
<span class="simpara"><strong><code>SORT_REGULAR</code></strong> - 按照通常方法比较(不修改类型)</span>
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="simpara"><strong><code>SORT_NUMERIC</code></strong> - 按照数字形式比较</span>
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="simpara"><strong><code>SORT_STRING</code></strong> - 按照字符串形式比较</span>
|
|
</li>
|
|
<li class="listitem">
|
|
<span class="simpara"><strong><code>SORT_LOCALE_STRING</code></strong> - 根据当前的本地化设置,按照字符串比较。
|
|
</span>
|
|
</li>
|
|
</ul>
|
|
</p>
|
|
</dd>
|
|
|
|
|
|
</dl>
|
|
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 returnvalues" id="refsect1-function.array-unique-returnvalues">
|
|
<h3 class="title">返回值</h3>
|
|
<p class="para">
|
|
返回过滤后的数组。
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 changelog" id="refsect1-function.array-unique-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>5.2.10</td>
|
|
<td>
|
|
修改回 <code class="parameter">sort_flags</code> 的默认值为
|
|
<strong><code>SORT_STRING</code></strong>。
|
|
</td>
|
|
</tr>
|
|
|
|
<tr>
|
|
<td>5.2.9</td>
|
|
<td>
|
|
增加可选选项<code class="parameter">sort_flags</code>,默认值 <strong><code>SORT_REGULAR</code></strong>。
|
|
5.2.9 之前,此函数内部使用 <strong><code>SORT_STRING</code></strong> 排序。
|
|
</td>
|
|
</tr>
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 examples" id="refsect1-function.array-unique-examples">
|
|
<h3 class="title">范例</h3>
|
|
<p class="para">
|
|
<div class="example" id="example-6077">
|
|
<p><strong>Example #1 <span class="function"><strong>array_unique()</strong></span> 例子</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$input </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"a" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"green"</span><span style="color: #007700">, </span><span style="color: #DD0000">"red"</span><span style="color: #007700">, </span><span style="color: #DD0000">"b" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"green"</span><span style="color: #007700">, </span><span style="color: #DD0000">"blue"</span><span style="color: #007700">, </span><span style="color: #DD0000">"red"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">array_unique</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">print_r</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
<div class="example-contents"><p>以上例程会输出:</p></div>
|
|
<div class="example-contents screen">
|
|
<div class="cdata"><pre>
|
|
Array
|
|
(
|
|
[a] => green
|
|
[0] => red
|
|
[1] => blue
|
|
)
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</p>
|
|
<p class="para">
|
|
<div class="example" id="example-6078">
|
|
<p><strong>Example #2 <span class="function"><strong>array_unique()</strong></span> 和类型</strong></p>
|
|
<div class="example-contents">
|
|
<div class="phpcode"><pre><span style="color: #000000">
|
|
<span style="color: #0000BB"><?php<br />$input </span><span style="color: #007700">= array(</span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #DD0000">"4"</span><span style="color: #007700">, </span><span style="color: #DD0000">"3"</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #DD0000">"3"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">array_unique</span><span style="color: #007700">(</span><span style="color: #0000BB">$input</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span>
|
|
</span>
|
|
</pre></div>
|
|
</div>
|
|
|
|
<div class="example-contents"><p>以上例程会输出:</p></div>
|
|
<div class="example-contents screen">
|
|
<div class="cdata"><pre>
|
|
array(2) {
|
|
[0] => int(4)
|
|
[2] => string(1) "3"
|
|
}
|
|
</pre></div>
|
|
</div>
|
|
</div>
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 seealso" id="refsect1-function.array-unique-seealso">
|
|
<h3 class="title">参见</h3>
|
|
<p class="para">
|
|
<ul class="simplelist">
|
|
<li class="member"><span class="function"><a href="array_count_values.html" class="function" rel="rdfs-seeAlso">array_count_values()</a> - 统计数组中所有的值</span></li>
|
|
</ul>
|
|
</p>
|
|
</div>
|
|
|
|
|
|
<div class="refsect1 notes" id="refsect1-function.array-unique-notes">
|
|
<h3 class="title">注释</h3>
|
|
<blockquote class="note"><p><strong class="note">Note</strong>:
|
|
<span class="simpara">
|
|
注意, <span class="function"><strong>array_unique()</strong></span> 不能应用于多维数组。
|
|
</span>
|
|
</p></blockquote>
|
|
</div>
|
|
|
|
</div></div></div></body></html> |