<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Computes the intersection of arrays using keys for comparison</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-intersect-assoc.html">array_intersect_assoc</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-intersect-uassoc.html">array_intersect_uassoc</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.array-intersect-key" class="refentry"> <div class="refnamediv"> <h1 class="refname">array_intersect_key</h1> <p class="verinfo">(PHP 5 >= 5.1.0)</p><p class="refpurpose"><span class="refname">array_intersect_key</span> — <span class="dc-title">Computes the intersection of arrays using keys for comparison</span></p> </div> <div class="refsect1 description" id="refsect1-function.array-intersect-key-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">array</span> <span class="methodname"><strong>array_intersect_key</strong></span> ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$array1</code></span> , <span class="methodparam"><span class="type">array</span> <code class="parameter">$array2</code></span> [, <span class="methodparam"><span class="type">array</span> <code class="parameter">$...</code></span> ] )</div> <p class="para rdfs-comment"> <span class="function"><strong>array_intersect_key()</strong></span> returns an array containing all the entries of <em><code class="parameter">array1</code></em> which have keys that are present in all the arguments. </p> </div> <div class="refsect1 parameters" id="refsect1-function.array-intersect-key-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">array1</code></em></span> <dd> <p class="para"> The array with master keys to check. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">array2</code></em></span> <dd> <p class="para"> An array to compare keys against. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">...</code></em></span> <dd> <p class="para"> A variable list of arrays to compare. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.array-intersect-key-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns an associative array containing all the entries of <em><code class="parameter">array1</code></em> which have keys that are present in all arguments. </p> </div> <div class="refsect1 examples" id="refsect1-function.array-intersect-key-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4950"> <p><strong>Example #1 <span class="function"><strong>array_intersect_key()</strong></span> example</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br />$array1 </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'blue' </span><span style="color: #007700">=> </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'red' </span><span style="color: #007700">=> </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #DD0000">'green' </span><span style="color: #007700">=> </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #DD0000">'purple' </span><span style="color: #007700">=> </span><span style="color: #0000BB">4</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$array2 </span><span style="color: #007700">= array(</span><span style="color: #DD0000">'green' </span><span style="color: #007700">=> </span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #DD0000">'blue' </span><span style="color: #007700">=> </span><span style="color: #0000BB">6</span><span style="color: #007700">, </span><span style="color: #DD0000">'yellow' </span><span style="color: #007700">=> </span><span style="color: #0000BB">7</span><span style="color: #007700">, </span><span style="color: #DD0000">'cyan' </span><span style="color: #007700">=> </span><span style="color: #0000BB">8</span><span style="color: #007700">);<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">array_intersect_key</span><span style="color: #007700">(</span><span style="color: #0000BB">$array1</span><span style="color: #007700">, </span><span style="color: #0000BB">$array2</span><span style="color: #007700">));<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> <div class="example-contents"><p>The above example will output:</p></div> <div class="example-contents screen"> <div class="cdata"><pre> array(2) { ["blue"]=> int(1) ["green"]=> int(3) } </pre></div> </div> </div> </p> <p class="para"> In our example you see that only the keys <em>'blue'</em> and <em>'green'</em> are present in both arrays and thus returned. Also notice that the values for the keys <em>'blue'</em> and <em>'green'</em> differ between the two arrays. A match still occurs because only the keys are checked. The values returned are those of <em><code class="parameter">array1</code></em>. </p> <p class="para"> The two keys from the <em>key => value</em> pairs are considered equal only if <em>(string) $key1 === (string) $key2 </em>. In other words a strict type check is executed so the string representation must be the same. </p> </div> <div class="refsect1 seealso" id="refsect1-function.array-intersect-key-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.array-diff.html" class="function" rel="rdfs-seeAlso">array_diff()</a> - Computes the difference of arrays</span></li> <li class="member"> <span class="function"><a href="function.array-udiff.html" class="function" rel="rdfs-seeAlso">array_udiff()</a> - Computes the difference of arrays by using a callback function for data comparison</span></li> <li class="member"> <span class="function"><a href="function.array-diff-assoc.html" class="function" rel="rdfs-seeAlso">array_diff_assoc()</a> - Computes the difference of arrays with additional index check</span></li> <li class="member"> <span class="function"><a href="function.array-diff-uassoc.html" class="function" rel="rdfs-seeAlso">array_diff_uassoc()</a> - Computes the difference of arrays with additional index check which is performed by a user supplied callback function</span></li> <li class="member"> <span class="function"><a href="function.array-udiff-assoc.html" class="function" rel="rdfs-seeAlso">array_udiff_assoc()</a> - Computes the difference of arrays with additional index check, compares data by a callback function</span></li> <li class="member"> <span class="function"><a href="function.array-udiff-uassoc.html" class="function" rel="rdfs-seeAlso">array_udiff_uassoc()</a> - Computes the difference of arrays with additional index check, compares data and indexes by a callback function</span></li> <li class="member"> <span class="function"><a href="function.array-diff-key.html" class="function" rel="rdfs-seeAlso">array_diff_key()</a> - Computes the difference of arrays using keys for comparison</span></li> <li class="member"> <span class="function"><a href="function.array-diff-ukey.html" class="function" rel="rdfs-seeAlso">array_diff_ukey()</a> - Computes the difference of arrays using a callback function on the keys for comparison</span></li> <li class="member"> <span class="function"><a href="function.array-intersect.html" class="function" rel="rdfs-seeAlso">array_intersect()</a> - Computes the intersection of arrays</span></li> <li class="member"> <span class="function"><a href="function.array-intersect-assoc.html" class="function" rel="rdfs-seeAlso">array_intersect_assoc()</a> - Computes the intersection of arrays with additional index check</span></li> <li class="member"> <span class="function"><a href="function.array-intersect-uassoc.html" class="function" rel="rdfs-seeAlso">array_intersect_uassoc()</a> - Computes the intersection of arrays with additional index check, compares indexes by a callback function</span></li> <li class="member"> <span class="function"><a href="function.array-intersect-ukey.html" class="function" rel="rdfs-seeAlso">array_intersect_ukey()</a> - Computes the intersection of arrays using a callback function on the keys for comparison</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.array-intersect-assoc.html">array_intersect_assoc</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.array-intersect-uassoc.html">array_intersect_uassoc</a></div> <div class="up"><a href="ref.array.html">Array Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>