mirror of
https://github.com/php/doc-zh.git
synced 2026-03-26 08:02:16 +01:00
git-svn-id: https://svn.php.net/repository/phpdoc/zh/trunk@327980 c90b9560-bf6c-de11-be94-00142212c4b1
225 lines
6.0 KiB
XML
Executable File
225 lines
6.0 KiB
XML
Executable File
<?xml version="1.0" encoding="utf-8"?>
|
||
<!-- $Revision$ -->
|
||
<!-- EN-Revision: d6b4563c0825092ca3e13700d76a34f527e8ba53 Maintainer: HonestQiao Status: ready -->
|
||
<!-- Reviewed: no Maintainer: HonestQiao -->
|
||
<refentry xml:id="function.sort" xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink">
|
||
<refnamediv>
|
||
<refname>sort</refname>
|
||
<refpurpose>对数组排序</refpurpose>
|
||
</refnamediv>
|
||
<refsect1 role="description">
|
||
&reftitle.description;
|
||
<methodsynopsis>
|
||
<type>bool</type><methodname>sort</methodname>
|
||
<methodparam><type>array</type><parameter role="reference">array</parameter></methodparam>
|
||
<methodparam choice="opt"><type>int</type><parameter>sort_flags</parameter><initializer>SORT_REGULAR</initializer></methodparam>
|
||
</methodsynopsis>
|
||
<para>
|
||
本函数对数组进行排序。当本函数结束时数组单元将被从最低到最高重新安排。
|
||
</para>
|
||
</refsect1>
|
||
<refsect1 role="parameters">
|
||
&reftitle.parameters;
|
||
<para>
|
||
<variablelist>
|
||
<varlistentry>
|
||
<term><parameter>array</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
要排序的数组。
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
<varlistentry>
|
||
<term><parameter>sort_flags</parameter></term>
|
||
<listitem>
|
||
<para>
|
||
可选的第二个参数 <parameter>sort_flags</parameter>
|
||
可以用以下值改变排序的行为:
|
||
</para>
|
||
<para>
|
||
排序类型标记:
|
||
<itemizedlist>
|
||
<listitem>
|
||
<simpara><constant>SORT_REGULAR</constant> - 正常比较单元(不改变类型)</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara><constant>SORT_NUMERIC</constant> - 单元被作为数字来比较</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara><constant>SORT_STRING</constant> - 单元被作为字符串来比较</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara> <constant>SORT_LOCALE_STRING</constant> -
|
||
根据当前的区域(locale)设置来把单元当作字符串比较,可以用
|
||
<function>setlocale</function> 来改变。
|
||
<!-- FIXME PHP_6
|
||
在
|
||
PHP 6 之前,使用了系统的区域设置,可以用
|
||
<function>setlocale</function> 来改变。自 PHP 6 起,必须用
|
||
<function>i18n_loc_set_default</function> 函数。
|
||
-->
|
||
</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara><constant>SORT_NATURAL</constant> - 和 <function>natsort</function> 类似对每个单元以“自然的顺序”对字符串进行排序。 PHP 5.4.0 中新增的。</simpara>
|
||
</listitem>
|
||
<listitem>
|
||
<simpara><constant>SORT_FLAG_CASE</constant> - 能够与 <constant>SORT_STRING</constant> 或
|
||
<constant>SORT_NATURAL</constant> 合并(OR 位运算),不区分大小写排序字符串。</simpara>
|
||
</listitem>
|
||
</itemizedlist>
|
||
</para>
|
||
</listitem>
|
||
</varlistentry>
|
||
</variablelist>
|
||
</para>
|
||
</refsect1>
|
||
<refsect1 role="returnvalues">
|
||
&reftitle.returnvalues;
|
||
<para>
|
||
&return.success;
|
||
</para>
|
||
</refsect1>
|
||
<refsect1 role="changelog">
|
||
&reftitle.changelog;
|
||
<informaltable>
|
||
<tgroup cols="2">
|
||
<thead>
|
||
<row>
|
||
<entry>&Version;</entry>
|
||
<entry>&Description;</entry>
|
||
</row>
|
||
</thead>
|
||
<tbody>
|
||
<row>
|
||
<entry>5.4.0</entry>
|
||
<entry>
|
||
添加了 <parameter>sort_flags</parameter> 内 <constant>SORT_NATURAL</constant> 和
|
||
<constant>SORT_FLAG_CASE</constant> 的支持。
|
||
</entry>
|
||
</row>
|
||
<row>
|
||
<entry>5.0.2</entry>
|
||
<entry>
|
||
添加了 <constant>SORT_LOCALE_STRING</constant>。
|
||
</entry>
|
||
</row>
|
||
</tbody>
|
||
</tgroup>
|
||
</informaltable>
|
||
</refsect1>
|
||
<refsect1 role="examples">
|
||
&reftitle.examples;
|
||
<para>
|
||
<example>
|
||
<title><function>sort</function> 例子</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
$fruits = array("lemon", "orange", "banana", "apple");
|
||
sort($fruits);
|
||
foreach ($fruits as $key => $val) {
|
||
echo "fruits[" . $key . "] = " . $val . "\n";
|
||
}
|
||
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
fruits[0] = apple
|
||
fruits[1] = banana
|
||
fruits[2] = lemon
|
||
fruits[3] = orange
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
fruits 被按照字母顺序排序。
|
||
</para>
|
||
<para>
|
||
<example>
|
||
<title>使用不区分大小写自然排序的 <function>sort</function> 例子</title>
|
||
<programlisting role="php">
|
||
<![CDATA[
|
||
<?php
|
||
|
||
$fruits = array(
|
||
"Orange1", "orange2", "Orange3", "orange20"
|
||
);
|
||
sort($fruits, SORT_NATURAL | SORT_FLAG_CASE);
|
||
foreach ($fruits as $key => $val) {
|
||
echo "fruits[" . $key . "] = " . $val . "\n";
|
||
}
|
||
|
||
?>
|
||
]]>
|
||
</programlisting>
|
||
&example.outputs;
|
||
<screen>
|
||
<![CDATA[
|
||
fruits[0] = Orange1
|
||
fruits[1] = orange2
|
||
fruits[2] = Orange3
|
||
fruits[3] = orange20
|
||
]]>
|
||
</screen>
|
||
</example>
|
||
</para>
|
||
<para>
|
||
fruits 排序得像 <function>natcasesort</function> 的结果。
|
||
</para>
|
||
</refsect1>
|
||
<refsect1 role="notes">
|
||
&reftitle.notes;
|
||
¬e.no-key-association;
|
||
<note>
|
||
<simpara>
|
||
和大多数 PHP 排序函数一样,<function>sort</function>
|
||
使用了 <link
|
||
xlink:href="&url.wiki.quicksort;">Quicksort</link> 实现的。
|
||
</simpara>
|
||
</note>
|
||
<warning>
|
||
<simpara>
|
||
在对含有混合类型值的数组排序时要小心,因为
|
||
<function>sort</function> 可能会产生不可预知的结果。
|
||
</simpara>
|
||
</warning>
|
||
</refsect1>
|
||
<refsect1 role="seealso">
|
||
&reftitle.seealso;
|
||
<para>
|
||
<simplelist>
|
||
<member><function>asort</function></member>
|
||
<member>&seealso.array.sorting;</member>
|
||
</simplelist>
|
||
</para>
|
||
</refsect1>
|
||
</refentry>
|
||
|
||
<!-- 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
|
||
sgml-default-dtd-file:"~/.phpdoc/manual.ced"
|
||
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
|
||
-->
|