Home  >  Article  >  php教程  >  Commonly used array functions in php (3) (functions to obtain array intersection array_intersect(), array_intersect_key(), array_intersect_assoc(), array_intersect_uassoc(), array_intersect_ukey())

Commonly used array functions in php (3) (functions to obtain array intersection array_intersect(), array_intersect_key(), array_intersect_assoc(), array_intersect_uassoc(), array_intersect_ukey())

WBOY
WBOYOriginal
2016-10-28 15:03:251575browse

These 5 functions for obtaining the intersection have 5 corresponding functions for obtaining the difference set. I am the link.


array_intersect($arr1, $arr2);

//Get the intersection of arrays with the same key value


array_intersect_key($arr1, $arr2);

//Get the intersection of arrays with the same key name


array_intersect_assoc(same as above);

//Get the intersection of arrays with the same key name and value


array_intersect_uassoc(same as above, 'custom callback function');

//Use custom callback function to obtain the intersection of arrays key values ​​with the same key name


array_intersect_ukey (same as above, custom callback function);

//Use custom callback function to obtain the intersection of data with the same key name


<span style="color: #008080"> 1</span> <span style="color: #800080">$arr1</span> = <span style="color: #0000ff">array</span>('r' => 'red','u' => 'blue', 'g' => 'green', 'b' => 'black'<span style="color: #000000">);
</span><span style="color: #008080"> 2</span> <span style="color: #800080">$arr2</span> = <span style="color: #0000ff">array</span>('r' => 'red', 'b' => 'blue'<span style="color: #000000">);
</span><span style="color: #008080"> 3</span> <span style="color: #008000">/*</span><span style="color: #008000">**********array_intersect(计算数组同键值的交集)****************</span><span style="color: #008000">*/</span>
<span style="color: #008080"> 4</span> <span style="color: #800080">$arrIntersect</span> = <span style="color: #008080">array_intersect</span>(<span style="color: #800080">$arr1</span>, <span style="color: #800080">$arr2</span><span style="color: #000000">);
</span><span style="color: #008080"> 5</span> <span style="color: #008080">var_dump</span>(<span style="color: #800080">$arrIntersect</span>);<span style="color: #008000">//</span><span style="color: #008000">=>array(2) { ["r"]=> string(3) "red" ["u"]=> string(4) "blue" }</span>
<span style="color: #008080"> 6</span> 
<span style="color: #008080"> 7</span> <span style="color: #008000">/*</span><span style="color: #008000">**********array_intersect_key(计算数组同键名的交集)****************</span><span style="color: #008000">*/</span>
<span style="color: #008080"> 8</span> <span style="color: #800080">$arrIntersectKey</span> = array_intersect_key(<span style="color: #800080">$arr1</span>, <span style="color: #800080">$arr2</span><span style="color: #000000">);
</span><span style="color: #008080"> 9</span> <span style="color: #008080">var_dump</span>(<span style="color: #800080">$arrIntersectKey</span>);<span style="color: #008000">//</span><span style="color: #008000">=>array(2) { ["r"]=> string(3) "red" ["b"]=> string(5) "black" }</span>
<span style="color: #008080">10</span> 
<span style="color: #008080">11</span> <span style="color: #008000">/*</span><span style="color: #008000">**********array_intersect_assoc(计算数组同键名同键值的交集)****************</span><span style="color: #008000">*/</span>
<span style="color: #008080">12</span> <span style="color: #800080">$arrIntersectAssoc</span> = <span style="color: #008080">array_intersect_assoc</span>(<span style="color: #800080">$arr1</span>, <span style="color: #800080">$arr2</span><span style="color: #000000">);
</span><span style="color: #008080">13</span> <span style="color: #008080">var_dump</span>(<span style="color: #800080">$arrIntersectAssoc</span>);<span style="color: #008000">//</span><span style="color: #008000">=>array(1) { ["r"]=> string(3) "red" }</span>
<span style="color: #008080">14</span> 
<span style="color: #008080">15</span> <span style="color: #008000">/*</span><span style="color: #008000">**********array_intersect_uassoc(用自定义的回调函数来计算数组同键名同键值的交集)****************</span><span style="color: #008000">*/</span>
<span style="color: #008080">16</span> <span style="color: #800080">$arrIntersectUassoc</span> = <span style="color: #008080">array_intersect_uassoc</span>(<span style="color: #800080">$arr1</span>, <span style="color: #800080">$arr2</span>, 'arr_intersect_uassoc_func'<span style="color: #000000">);
</span><span style="color: #008080">17</span> <span style="color: #008080">var_dump</span>(<span style="color: #800080">$arrIntersectUassoc</span>);<span style="color: #008000">//</span><span style="color: #008000">=>array(1) { ["r"]=> string(3) "red" }</span>
<span style="color: #008080">18</span> 
<span style="color: #008080">19</span> <span style="color: #0000ff">function</span> arr_intersect_uassoc_func(<span style="color: #800080">$a</span>, <span style="color: #800080">$b</span><span style="color: #000000">) {
</span><span style="color: #008080">20</span>     <span style="color: #0000ff">if</span> (<span style="color: #800080">$a</span> === <span style="color: #800080">$b</span><span style="color: #000000">)
</span><span style="color: #008080">21</span>         <span style="color: #0000ff">return</span> 0<span style="color: #000000">;
</span><span style="color: #008080">22</span>     <span style="color: #0000ff">elseif</span> (<span style="color: #800080">$a</span> > <span style="color: #800080">$b</span><span style="color: #000000">)
</span><span style="color: #008080">23</span>         <span style="color: #0000ff">return</span> 1<span style="color: #000000">;
</span><span style="color: #008080">24</span>     <span style="color: #0000ff">else</span>
<span style="color: #008080">25</span>         <span style="color: #0000ff">return</span> -1<span style="color: #000000">;
</span><span style="color: #008080">26</span> <span style="color: #000000">}
</span><span style="color: #008080">27</span> 
<span style="color: #008080">28</span> <span style="color: #008000">/*</span><span style="color: #008000">**********array_intersect_ukey(用自定义的回调函数来计算数组同键名的交集)****************</span><span style="color: #008000">*/</span>
<span style="color: #008080">29</span> <span style="color: #800080">$arrIntersectUkey</span> = array_intersect_ukey(<span style="color: #800080">$arr1</span>, <span style="color: #800080">$arr2</span>, 'arr_intersect_ukey_func'<span style="color: #000000">);
</span><span style="color: #008080">30</span> <span style="color: #008080">var_dump</span>(<span style="color: #800080">$arrIntersectUkey</span>);<span style="color: #008000">//</span><span style="color: #008000">=>array(2) { ["r"]=> string(3) "red" ["b"]=> string(5) "black" }</span>
<span style="color: #008080">31</span> 
<span style="color: #008080">32</span> <span style="color: #0000ff">function</span> arr_intersect_ukey_func(<span style="color: #800080">$k1</span>, <span style="color: #800080">$k2</span><span style="color: #000000">) {
</span><span style="color: #008080">33</span>     <span style="color: #0000ff">if</span> (<span style="color: #800080">$k1</span> == <span style="color: #800080">$k2</span><span style="color: #000000">)
</span><span style="color: #008080">34</span>         <span style="color: #0000ff">return</span> 0<span style="color: #000000">;
</span><span style="color: #008080">35</span>     <span style="color: #0000ff">elseif</span> (<span style="color: #800080">$k1</span> > <span style="color: #800080">$k2</span><span style="color: #000000">)
</span><span style="color: #008080">36</span>         <span style="color: #0000ff">return</span> 1<span style="color: #000000">;
</span><span style="color: #008080">37</span>     <span style="color: #0000ff">else</span>
<span style="color: #008080">38</span>         <span style="color: #0000ff">return</span> -1<span style="color: #000000">;
</span><span style="color: #008080">39</span> }

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn