Rumah >php教程 >php手册 >php中的常用数组函数(三)(获取数组交集的函数们 array_intersect()、array_intersect_key()、array_intersect_assoc()、array_intersect_uassoc()、array_intersect_ukey())

php中的常用数组函数(三)(获取数组交集的函数们 array_intersect()、array_intersect_key()、array_intersect_assoc()、array_intersect_uassoc()、array_intersect_ukey())

WBOY
WBOYasal
2016-10-28 15:03:251604semak imbas

这5个获取交集的函数 有 5个对应的获取差集的函数。我是链接。


array_intersect($arr1, $arr2);

//获得数组同键值的交集


array_intersect_key($arr1, $arr2);

//获得数组同键名的交集


array_intersect_assoc(同上);

//获得数组同键名键值的交集


array_intersect_uassoc(同上,'自定义回调函数');

//用自定义的回调函数来获得数组同键名键值的交集


array_intersect_ukey(同上, 自定义回调函数);

//用自定义的回调函数来获得数据同键名的交集


<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> }

 

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn