com doc/zh: Update array-filter.xml: reference/array/functions/array-filter.xml
[email protected] (CHU Zhaowei) Mon, 18 Jan 2021 07:25:23 +0000
| Newsgroups | php.doc.zh |
|---|---|
| Message-ID | <[email protected]> |
Commit: 5478f8251ac380dd778732fb086902344b99bf81 Author: 蒋文健 <[email protected]> Mon, 18 Jan 2021 12:29:20 +0800 Committer: CHU Zhaowei <[email protected]> Mon, 18 Jan 2021 15:25:23 +0800 Parents: c19169837f9adc2898ea5b6c3b8b4e3b360d7650 Branches: master Link: http://git.php.net/?p=doc/zh.git;a=commitdiff;h=5478f8251ac380dd778732fb086902344b99bf81 Log: Update array-filter.xml 同步英文版最新内容,并翻译 Changed paths: M reference/array/functions/array-filter.xml
diff_5478f8251ac380dd778732fb086902344b99bf81.txt
(text/plain, 8.2 KB)
diff --git a/reference/array/functions/array-filter.xml b/reference/array/functions/array-filter.xml
index f3ad464a..f0a6fb1b 100755
--- a/reference/array/functions/array-filter.xml
+++ b/reference/array/functions/array-filter.xml
@@ -1,29 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
-<!-- EN-Revision: 27acde0b15f7131bbc5239a14073d63307770b18 Maintainer: HonestQiao Status: ready -->
+<!-- EN-Revision: cd943f94a013b74df8765ab8e1a620a916a64a85 Maintainer: HonestQiao Status: ready -->
<refentry xml:id="function.array-filter" xmlns="http://docbook.org/ns/docbook">
- <refnamediv>
- <refname>array_filter</refname>
- <refpurpose>
- 用回调函数过滤数组中的单元
- </refpurpose>
- </refnamediv>
+ <refnamediv>
+ <refname>array_filter</refname>
+ <refpurpose>使用回调函数过滤数组的元素</refpurpose>
+ </refnamediv>
+
<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>array</type><methodname>array_filter</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
- <methodparam choice="opt"><type>callable</type><parameter>callback</parameter></methodparam>
- <methodparam choice="opt"><type>int</type><parameter>flag</parameter><initializer>0</initializer></methodparam>
+ <methodparam choice="opt"><type class="union"><type>callable</type><type>null</type></type><parameter>callback</parameter><initializer>&null;</initializer></methodparam>
+ <methodparam choice="opt"><type>int</type><parameter>mode</parameter><initializer>0</initializer></methodparam>
</methodsynopsis>
- <para>
- 依次将 <parameter>array</parameter> 数组中的每个值传递到
- <parameter>callback</parameter> 函数。如果
- <parameter>callback</parameter> 函数返回 true,则
- <parameter>array</parameter>
- 数组的当前值会被包含在返回的结果数组中。数组的键名保留不变。
- </para>
- </refsect1>
+ <para>
+ 遍历 <parameter>array</parameter> 数组中的每个值,并将每个值传递给 <parameter>callback</parameter> 回调函数。
+ 如果 <parameter>callback</parameter> 回调函数返回 &true;,则将 <parameter>array</parameter> 数组中的当前值返回到结果 &array; 数组中。
+ </para>
+ <para>
+ 返回结果 &array; 数组的键名(下标)会维持不变,所以返回的结果 &array; 数组键名(下标)可能会不连续。
+ 可以使用 <function>array_values</function> 函数对数组重新索引。
+ </para>
+</refsect1>
<refsect1 role="parameters">
&reftitle.parameters;
@@ -34,7 +34,7 @@
<term><parameter>array</parameter></term>
<listitem>
<para>
- 要循环的数组
+ 要遍历的数组
</para>
</listitem>
</varlistentry>
@@ -46,28 +46,26 @@
使用的回调函数
</para>
<para>
- 如果没有提供 <parameter>callback</parameter>
- 函数,
- 将删除 <parameter>array</parameter> 中所有等值为
- &false; 的条目。更多信息见<link
- linkend="language.types.boolean.casting">转换为布尔值</link>。
+ 如果没有提供 <parameter>callback</parameter> 回调函数,将删除数组中 <parameter>array</parameter> 的所有“空”元素。
+ 有关 PHP 如何判定“空”元素,请参阅 <function>empty</function> 。
</para>
</listitem>
</varlistentry>
<varlistentry>
- <term><parameter>flag</parameter></term>
+ <term><parameter>mode</parameter></term>
<listitem>
<para>
- 决定<parameter>callback</parameter>接收的参数形式:
+ 决定哪些参数发送到 <parameter>callback</parameter> 回调的标志:
<itemizedlist>
<listitem>
- <simpara><constant>ARRAY_FILTER_USE_KEY</constant> - <parameter>callback</parameter>接受键名作为的唯一参数</simpara>
+ <simpara><constant>ARRAY_FILTER_USE_KEY</constant> - 将键名作为 <parameter>callback</parameter> 回调的唯一参数,而不是值</simpara>
</listitem>
<listitem>
- <simpara><constant>ARRAY_FILTER_USE_BOTH</constant> - <parameter>callback</parameter>同时接受键名和键值</simpara>
+ <simpara><constant>ARRAY_FILTER_USE_BOTH</constant> - 将值和键都作为参数传递给 <parameter>callback</parameter> 回调,而不是仅传递值</simpara>
</listitem>
</itemizedlist>
+ 默认值为 <literal>0</literal> ,只传递值作为 <parameter>callback</parameter> 回调的唯一参数。
</para>
</listitem>
</varlistentry>
@@ -83,7 +81,6 @@
</para>
</refsect1>
-
<refsect1 role="changelog">
&reftitle.changelog;
<informaltable>
@@ -96,18 +93,24 @@
</thead>
<tbody>
<row>
+ <entry>8.0.0</entry>
+ <entry>
+ <parameter>callback</parameter> 回调现在可以为 &null;。
+ </entry>
+ </row>
+ <row>
<entry>5.6.0</entry>
<entry>
- 添加可选的参数 <parameter>flag</parameter>,以及常量
- <constant>ARRAY_FILTER_USE_KEY</constant> 和
- <constant>ARRAY_FILTER_USE_BOTH</constant>。
+ 添加可选的 <parameter>mode</parameter> 标志和常量(
+ <constant>ARRAY_FILTER_USE_KEY</constant> 和
+ <constant>ARRAY_FILTER_USE_BOTH</constant> )
</entry>
</row>
</tbody>
</tgroup>
</informaltable>
</refsect1>
-
+
<!--
<refsect1 role="errors">
&reftitle.errors;
@@ -119,25 +122,22 @@
&reftitle.examples;
<para>
<example>
- <title><function>array_filter</function> 例子</title>
+ <title><function>array_filter</function> 示例</title>
<programlisting role="php">
<![CDATA[
<?php
function odd($var)
{
- // returns whether the input integer is odd
- return($var & 1);
+ // 返回输入整数是否为奇数(单数)
+ return $var & 1;
}
-
function even($var)
{
- // returns whether the input integer is even
- return(!($var & 1));
+ // 返回输入整数是否为偶数
+ return !($var & 1);
}
-
-$array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
-$array2 = array(6, 7, 8, 9, 10, 11, 12);
-
+$array1 = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];
+$array2 = [6, 7, 8, 9, 10, 11, 12];
echo "Odd :\n";
print_r(array_filter($array1, "odd"));
echo "Even:\n";
@@ -167,20 +167,19 @@ Array
</screen>
</example>
<example>
- <title>不使用 <parameter>callback</parameter> 时的<function>array_filter</function>
- </title>
+ <title><function>array_filter</function> 不使用 <parameter>callback</parameter> 参数时</title>
<programlisting role="php">
<![CDATA[
<?php
-
-$entry = array(
- 0 => 'foo',
- 1 => false,
- 2 => -1,
- 3 => null,
- 4 => ''
- );
-
+$entry = [
+ 0 => 'foo',
+ 1 => false,
+ 2 => -1,
+ 3 => null,
+ 4 => '',
+ 5 => '0',
+ 6 => 0,
+];
print_r(array_filter($entry));
?>
]]>
@@ -197,18 +196,14 @@ Array
</screen>
</example>
<example>
- <title>带 <parameter>flag</parameter> 标记的 <function>array_filter</function>
- </title>
+ <title><function>array_filter</function> 不使用 <parameter>mode</parameter> 参数时</title>
<programlisting role="php">
<![CDATA[
<?php
-
$arr = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4];
-
var_dump(array_filter($arr, function($k) {
return $k == 'b';
}, ARRAY_FILTER_USE_KEY));
-
var_dump(array_filter($arr, function($v, $k) {
return $k == 'b' || $v == 4;
}, ARRAY_FILTER_USE_BOTH));
@@ -238,9 +233,7 @@ array(2) {
&reftitle.notes;
<caution>
<para>
- 用户不应在回调函数中修改数组本身。例如增加/删除单元或者对
- <function>array_filter</function> 正在作用的数组进行
- unset。如果数组改变了,此函数的行为将不可预测。
+ 不应该在回调函数中改变数组(如:添加、删除或销毁“unset”当前数组和里面的元素),否则此函数的行为将不可预测。
</para>
</caution>
</refsect1>
@@ -255,8 +248,8 @@ array(2) {
</simplelist>
</para>
</refsect1>
- </refentry>
+</refentry>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml