교점을 구하는 이 5가지 함수에는 차이 집합을 구하는 5개의 해당 함수가 있습니다. 제가 바로 링크입니다.
//같은 키 값을 갖는 배열 의 교집합을 구합니다.
//키 이름이 같은 배열 의 교집합을 가져옵니다.
//키 이름과 값이 동일한 배열
의 교집합을 가져옵니다.//사용자 정의 콜백 함수 를 사용하여 동일한 키 이름과 값 을 사용하여 배열 의 교집합
을 얻습니다.//사용자 정의 콜백 함수를 사용하여 동일한 키 이름
을 가진 데이터의 교집합을 얻습니다.<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> }