📄 language.operators.array.html
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html> <head> <title>Array Operators</title> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head> <body><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.operators.string.html">String Operators</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.operators.type.html">Type Operators</a></div> <div class="up"><a href="language.operators.html">Operators</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div><hr /><div id="language.operators.array" class="sect1"> <h2 class="title">Array Operators</h2> <table border="5"> <caption><b>Array Operators</b></caption> <colgroup> <thead valign="middle"> <tr valign="middle"> <th colspan="1">Example</th> <th colspan="1">Name</th> <th colspan="1">Result</th> </tr> </thead> <tbody valign="middle" class="tbody"> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">$a + $b</td> <td colspan="1" rowspan="1" align="left">Union</td> <td colspan="1" rowspan="1" align="left">Union of $a and $b.</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">$a == $b</td> <td colspan="1" rowspan="1" align="left">Equality</td> <td colspan="1" rowspan="1" align="left"><b><tt>TRUE</tt></b> if $a and $b have the same key/value pairs.</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">$a === $b</td> <td colspan="1" rowspan="1" align="left">Identity</td> <td colspan="1" rowspan="1" align="left"><b><tt>TRUE</tt></b> if $a and $b have the same key/value pairs in the same order and of the same types.</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">$a != $b</td> <td colspan="1" rowspan="1" align="left">Inequality</td> <td colspan="1" rowspan="1" align="left"><b><tt>TRUE</tt></b> if $a is not equal to $b.</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">$a <> $b</td> <td colspan="1" rowspan="1" align="left">Inequality</td> <td colspan="1" rowspan="1" align="left"><b><tt>TRUE</tt></b> if $a is not equal to $b.</td> </tr> <tr valign="middle"> <td colspan="1" rowspan="1" align="left">$a !== $b</td> <td colspan="1" rowspan="1" align="left">Non-identity</td> <td colspan="1" rowspan="1" align="left"><b><tt>TRUE</tt></b> if $a is not identical to $b.</td> </tr> </tbody> </colgroup> </table> <p class="para"> The <i>+</i> operator appends elements of remaining keys from the right handed array to the left handed, whereas duplicated keys are NOT overwritten. </p> <p class="para"> <div class="informalexample"> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"a" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"apple"</span><span style="color: #007700">, </span><span style="color: #DD0000">"b" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"banana"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"a" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"pear"</span><span style="color: #007700">, </span><span style="color: #DD0000">"b" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"strawberry"</span><span style="color: #007700">, </span><span style="color: #DD0000">"c" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"cherry"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">$a </span><span style="color: #007700">+ </span><span style="color: #0000BB">$b</span><span style="color: #007700">; </span><span style="color: #FF8000">// Union of $a and $b<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Union of \$a and \$b: \n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">$c </span><span style="color: #007700">= </span><span style="color: #0000BB">$b </span><span style="color: #007700">+ </span><span style="color: #0000BB">$a</span><span style="color: #007700">; </span><span style="color: #FF8000">// Union of $b and $a<br /></span><span style="color: #007700">echo </span><span style="color: #DD0000">"Union of \$b and \$a: \n"</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$c</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> When executed, this script will print the following: <div class="example-contents"><pre><div class="cdata"><pre>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"}</pre></div> </pre></div> </p> <p class="para"> Elements of arrays are equal for the comparison if they have the same key and value. </p> <p class="para"> <div class="example"> <p><b>Example #1 Comparing arrays</b></p> <div class="example-contents"><div class="phpcode"><code><span style="color: #000000"><span style="color: #0000BB"><?php<br />$a </span><span style="color: #007700">= array(</span><span style="color: #DD0000">"apple"</span><span style="color: #007700">, </span><span style="color: #DD0000">"banana"</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$b </span><span style="color: #007700">= array(</span><span style="color: #0000BB">1 </span><span style="color: #007700">=> </span><span style="color: #DD0000">"banana"</span><span style="color: #007700">, </span><span style="color: #DD0000">"0" </span><span style="color: #007700">=> </span><span style="color: #DD0000">"apple"</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a </span><span style="color: #007700">== </span><span style="color: #0000BB">$b</span><span style="color: #007700">); </span><span style="color: #FF8000">// bool(true)<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">$a </span><span style="color: #007700">=== </span><span style="color: #0000BB">$b</span><span style="color: #007700">); </span><span style="color: #FF8000">// bool(false)<br /></span><span style="color: #0000BB">?></span></span></code></div> </div> </div> </p> <p class="para"> See also the manual sections on the <a href="language.types.array.html" class="link">Array type</a> and <a href="ref.array.html" class="link">Array functions</a>. </p> </div><hr /><div style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="language.operators.string.html">String Operators</a></div> <div class="next" style="text-align: right; float: right;"><a href="language.operators.type.html">Type Operators</a></div> <div class="up"><a href="language.operators.html">Operators</a></div> <div class="home"><a href="index.html">PHP Manual</a></div></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -