>php教程 >php手册 >PHP에서 일반적으로 사용되는 배열 함수 (3) (배열 교차점을 얻는 함수 array_intersect(), array_intersect_key(), array_intersect_assoc(), array_intersect_uassoc(), array_intersect_ukey())

PHP에서 일반적으로 사용되는 배열 함수 (3) (배열 교차점을 얻는 함수 array_intersect(), array_intersect_key(), array_intersect_assoc(), array_intersect_uassoc(), array_intersect_ukey())

WBOY
WBOY원래의
2016-10-28 15:03:251604검색

교점을 구하는 이 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> }

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.