Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion reference/array/functions/array-all.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
<term><parameter>array</parameter></term>
<listitem>
<simpara>
需要被遍历的 &array;
需要被遍历的 &array;
</simpara>
</listitem>
</varlistentry>
Expand Down
130 changes: 130 additions & 0 deletions reference/array/functions/array-any.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 679cf93fa1e54cde82fc9cf545966eb13bcb0638 Maintainer: Singi Status: ready -->
<refentry xml:id="function.array-any" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>array_any</refname>
<refpurpose>检查数组中是否至少有一个元素满足回调函数的条件</refpurpose>
</refnamediv>

<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>bool</type><methodname>array_any</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam><type>callable</type><parameter>callback</parameter></methodparam>
</methodsynopsis>
<simpara>
如果指定的 <parameter>callback</parameter> 对任意一个元素返回 &true;,则 <function>array_any</function> 函数返回 &true;;否则该函数返回 &false;。
</simpara>
</refsect1>

<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>array</parameter></term>
<listitem>
<simpara>
需要被遍历的 &array;。
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>callback</parameter></term>
<listitem>
<para>
用于检查每个元素的回调函数,该函数必须
<methodsynopsis>
<type>bool</type><methodname><replaceable>callback</replaceable></methodname>
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
<methodparam><type>mixed</type><parameter>key</parameter></methodparam>
</methodsynopsis>
如果该函数返回 &true;,则 <function>array_any</function> 会立即返回 &true;,并且不会再为后续元素调用该回调函数。
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>

<refsect1 role="returnvalues">
&reftitle.returnvalues;
<simpara>
如果数组中至少存在一个元素能让 <parameter>callback</parameter> 返回 &true;,则该函数返回 &true;;否则返回 &false;。
</simpara>
</refsect1>

<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>array_any</function> 示例</title>
<programlisting role="php">
<![CDATA[
<?php
$array = [
'a' => 'dog',
'b' => 'cat',
'c' => 'cow',
'd' => 'duck',
'e' => 'goose',
'f' => 'elephant'
];

// Check, if any animal name is longer than 5 letters.
var_dump(array_any($array, function (string $value) {
return strlen($value) > 5;
}));

// Check, if any animal name is shorter than 3 letters.
var_dump(array_any($array, function (string $value) {
return strlen($value) < 3;
}));

// Check, if any array key is not a string.
var_dump(array_any($array, function (string $value, $key) {
return !is_string($key);
}));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
bool(true)
bool(false)
bool(false)
]]>
</screen>
</example>
</refsect1>

<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><function>array_all</function></member>
<member><function>array_filter</function></member>
<member><function>array_find</function></member>
<member><function>array_find_key</function></member>
</simplelist>
</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
-->
137 changes: 137 additions & 0 deletions reference/array/functions/array-find-key.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- $Revision$ -->
<!-- EN-Revision: 596c11440dc232b8ed1836d7e3afe2ed5b225a7b Maintainer: Singi Status: ready -->
<refentry xml:id="function.array-find-key" xmlns="http://docbook.org/ns/docbook">
<refnamediv>
<refname>array_find_key</refname>
<refpurpose>返回满足回调函数条件的第一个元素的键</refpurpose>
</refnamediv>

<refsect1 role="description">
&reftitle.description;
<methodsynopsis>
<type>mixed</type><methodname>array_find_key</methodname>
<methodparam><type>array</type><parameter>array</parameter></methodparam>
<methodparam><type>callable</type><parameter>callback</parameter></methodparam>
</methodsynopsis>
<simpara>
<function>array_find_key</function> 返回 &array; 中第一个使指定 <parameter>callback</parameter> 返回 &true; 的元素的键。如果未找到匹配元素,则该函数返回 &null;。
</simpara>
</refsect1>

<refsect1 role="parameters">
&reftitle.parameters;
<variablelist>
<varlistentry>
<term><parameter>array</parameter></term>
<listitem>
<simpara>
需要被遍历的 &array;。
</simpara>
</listitem>
</varlistentry>
<varlistentry>
<term><parameter>callback</parameter></term>
<listitem>
<para>
用于检查每个元素的回调函数,该函数必须
<methodsynopsis>
<type>bool</type><methodname><replaceable>callback</replaceable></methodname>
<methodparam><type>mixed</type><parameter>value</parameter></methodparam>
<methodparam><type>mixed</type><parameter>key</parameter></methodparam>
</methodsynopsis>
如果该函数返回 &true;,<function>array_find_key</function> 就会返回对应键名,并且不再对后续元素执行该回调函数。
</para>
</listitem>
</varlistentry>
</variablelist>
</refsect1>

<refsect1 role="returnvalues">
&reftitle.returnvalues;
<simpara>
该函数会返回首个使 <parameter>callback</parameter> 返回 &true; 的元素的键。如果未找到匹配的元素,则函数返回 &null;。
</simpara>
</refsect1>

<refsect1 role="examples">
&reftitle.examples;
<example>
<title><function>array_find_key</function> 示例</title>
<programlisting role="php">
<![CDATA[
<?php
$array = [
'a' => 'dog',
'b' => 'cat',
'c' => 'cow',
'd' => 'duck',
'e' => 'goose',
'f' => 'elephant'
];

// Find the first animal with a name longer than 4 characters.
var_dump(array_find_key($array, function (string $value) {
return strlen($value) > 4;
}));

// Find the first animal whose name begins with f.
var_dump(array_find_key($array, function (string $value) {
return str_starts_with($value, 'f');
}));

// Find the first animal where the array key is the first symbol of the animal.
var_dump(array_find_key($array, function (string $value, $key) {
return $value[0] === $key;
}));

// Find the first animal where the array key matching a RegEx.
var_dump(array_find_key($array, function ($value, $key) {
return preg_match('/^([a-f])$/', $key);
}));
?>
]]>
</programlisting>
&example.outputs;
<screen>
<![CDATA[
string(1) "e"
NULL
string(1) "c"
string(1) "a"
]]>
</screen>
</example>
</refsect1>

<refsect1 role="seealso">
&reftitle.seealso;
<simplelist>
<member><function>array_find</function></member>
<member><function>array_all</function></member>
<member><function>array_any</function></member>
<member><function>array_filter</function></member>
<member><function>array_reduce</function></member>
</simplelist>
</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
-->
Loading