1
0
mirror of https://github.com/php/doc-en.git synced 2026-03-23 23:32:18 +01:00
Files
2025-04-09 11:36:28 +01:00

144 lines
3.3 KiB
XML

<?xml version="1.0" encoding="utf-8"?>
<sect1 xml:id="language.operators.array">
<title>Array Operators</title>
<titleabbrev>Array</titleabbrev>
<table>
<title>Array Operators</title>
<tgroup cols="3">
<thead>
<row>
<entry>Example</entry>
<entry>Name</entry>
<entry>Result</entry>
</row>
</thead>
<tbody>
<row>
<entry>$a + $b</entry>
<entry>Union</entry>
<entry>Union of <varname>$a</varname> and <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a == $b</entry>
<entry>Equality</entry>
<entry>&true; if <varname>$a</varname> and <varname>$b</varname> have the same key/value pairs.</entry>
</row>
<row>
<entry>$a === $b</entry>
<entry>Identity</entry>
<entry>&true; if <varname>$a</varname> and <varname>$b</varname> have the same key/value pairs in the same
order and of the same types.</entry>
</row>
<row>
<entry>$a != $b</entry>
<entry>Inequality</entry>
<entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a &lt;&gt; $b</entry>
<entry>Inequality</entry>
<entry>&true; if <varname>$a</varname> is not equal to <varname>$b</varname>.</entry>
</row>
<row>
<entry>$a !== $b</entry>
<entry>Non-identity</entry>
<entry>&true; if <varname>$a</varname> is not identical to <varname>$b</varname>.</entry>
</row>
</tbody>
</tgroup>
</table>
<para>
The <literal>+</literal> operator returns the right-hand array appended
to the left-hand array; for keys that exist in both arrays, the elements
from the left-hand array will be used, and the matching elements from the
right-hand array will be ignored.
</para>
<para>
<example>
<title>Array Append Operator</title>
<programlisting role="php">
<![CDATA[
<?php
$a = array("a" => "apple", "b" => "banana");
$b = array("a" => "pear", "b" => "strawberry", "c" => "cherry");
$c = $a + $b; // Union of $a and $b
echo "Union of \$a and \$b: \n";
var_dump($c);
$c = $b + $a; // Union of $b and $a
echo "Union of \$b and \$a: \n";
var_dump($c);
$a += $b; // Union of $a += $b is $a and $b
echo "Union of \$a += \$b: \n";
var_dump($a);
?>
]]>
</programlisting>
&example.outputs;
<screen role="php">
<![CDATA[
Union of $a and $b:
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(6) "cherry"
}
Union of $b and $a:
array(3) {
["a"]=>
string(4) "pear"
["b"]=>
string(10) "strawberry"
["c"]=>
string(6) "cherry"
}
Union of $a += $b:
array(3) {
["a"]=>
string(5) "apple"
["b"]=>
string(6) "banana"
["c"]=>
string(6) "cherry"
}
]]>
</screen>
</example>
</para>
<para>
Elements of arrays are equal for the comparison if they have the
same key and value.
</para>
<para>
<example>
<title>Comparing arrays</title>
<programlisting role="php">
<![CDATA[
<?php
$a = array("apple", "banana");
$b = array(1 => "banana", "0" => "apple");
var_dump($a == $b); // bool(true)
var_dump($a === $b); // bool(false)
?>
]]>
</programlisting>
</example>
</para>
<sect2 role="seealso">
&reftitle.seealso;
<para>
<simplelist>
<member><link linkend="language.types.array">Array type</link></member>
<member><link linkend="ref.array">Array functions</link></member>
</simplelist>
</para>
</sect2>
</sect1>