Heim  >  Artikel  >  php教程  >  Häufig verwendete Array-Funktionen in PHP (3) (Funktionen zum Erhalten von Array-Schnittpunkten array_intersect(), array_intersect_key(), array_intersect_assoc(), array_intersect_uassoc(), array_intersect_ukey())

Häufig verwendete Array-Funktionen in PHP (3) (Funktionen zum Erhalten von Array-Schnittpunkten array_intersect(), array_intersect_key(), array_intersect_assoc(), array_intersect_uassoc(), array_intersect_ukey())

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

Diese fünf Funktionen zum Erhalten des Schnittpunkts haben 5 entsprechende Funktionen zum Erhalten des Differenzsatzes. Ich bin der Link.


array_intersect($arr1, $arr2);

//Den Schnittpunkt des Arrays mit demselben Schlüsselwert

ermitteln

array_intersect_key($arr1, $arr2);

//Den Schnittpunkt des Arrays mit demselben Schlüsselnamen

ermitteln

array_intersect_assoc(wie oben);

//Ermitteln Sie den Schnittpunkt des Arrays mit demselben Schlüsselnamen und Wert


array_intersect_uassoc(wie oben, 'benutzerdefinierte Rückruffunktion');

//Verwenden Sie die benutzerdefinierte Rückruffunktion , um die Schnittmenge des Arrays mit demselben Schlüsselnamen und Wert

zu erhalten

array_intersect_ukey (wie oben, benutzerdefinierte Rückruffunktion);

//Verwenden Sie die benutzerdefinierte Rückruffunktion , um die Schnittmenge von Daten mit demselben Schlüsselnamen

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn