Home >Backend Development >PHP Tutorial >PHP 过滤二维数组跟三维数组
PHP 过滤二维数组和三维数组
<span style="color: #008080;"> 1</span> <span style="color: #000000;">php</span><span style="color: #008080;"> 2</span> <span style="color: #008080;"> 3</span> <span style="color: #800080;">$arr</span> =<span style="color: #000000;"> [</span><span style="color: #008080;"> 4</span> [1,3,5,7,9],<span style="color: #008080;"> 5</span> [2,4,6,8,0<span style="color: #000000;">]</span><span style="color: #008080;"> 6</span> <span style="color: #000000;"> ];</span><span style="color: #008080;"> 7</span> <span style="color: #800080;">$arr2</span> =<span style="color: #000000;"> [</span><span style="color: #008080;"> 8</span> 'list' =><span style="color: #000000;"> [</span><span style="color: #008080;"> 9</span> [1,3,5,7],<span style="color: #008080;">10</span> [2,4,6,8],<span style="color: #008080;">11</span> [3,2,9,0],<span style="color: #008080;">12</span> <span style="color: #000000;"> ]</span><span style="color: #008080;">13</span> <span style="color: #000000;"> ];</span><span style="color: #008080;">14</span> <span style="color: #800080;">$arr3</span> =<span style="color: #000000;"> [</span><span style="color: #008080;">15</span> 'list' =><span style="color: #000000;"> [</span><span style="color: #008080;">16</span> [1,3,5,7],<span style="color: #008080;">17</span> [2,4,6,8],<span style="color: #008080;">18</span> [3,2,9,0<span style="color: #000000;">]</span><span style="color: #008080;">19</span> <span style="color: #000000;"> ]</span><span style="color: #008080;">20</span> <span style="color: #000000;"> ];</span><span style="color: #008080;">21</span> <span style="color: #008080;">22</span> <span style="color: #800080;">$res</span> = <span style="color: #008080;">array_map</span>(<span style="color: #0000ff;">function</span>(<span style="color: #800080;">$arr</span><span style="color: #000000;">){</span><span style="color: #008080;">23</span> <span style="color: #0000ff;">return</span> <span style="color: #008080;">array_filter</span>(<span style="color: #800080;">$arr</span>, <span style="color: #0000ff;">function</span>(<span style="color: #800080;">$v</span><span style="color: #000000;">) {</span><span style="color: #008080;">24</span> <span style="color: #0000ff;">return</span> <span style="color: #800080;">$v</span> !== 5<span style="color: #000000;">;</span><span style="color: #008080;">25</span> <span style="color: #000000;"> });</span><span style="color: #008080;">26</span> }, <span style="color: #800080;">$arr</span><span style="color: #000000;">);</span><span style="color: #008080;">27</span> <span style="color: #008080;">28</span> <span style="color: #800080;">$res2</span> = <span style="color: #008080;">array_map</span>(<span style="color: #0000ff;">function</span>(<span style="color: #800080;">$val</span><span style="color: #000000;">) {</span><span style="color: #008080;">29</span> <span style="color: #0000ff;">return</span> <span style="color: #008080;">array_filter</span>(<span style="color: #800080;">$val</span>, <span style="color: #0000ff;">function</span>(<span style="color: #800080;">$v</span><span style="color: #000000;">) {</span><span style="color: #008080;">30</span> <span style="color: #0000ff;">return</span> !<span style="color: #008080;">in_array</span>(6, <span style="color: #800080;">$v</span><span style="color: #000000;">);</span><span style="color: #008080;">31</span> <span style="color: #000000;"> });</span><span style="color: #008080;">32</span> }, <span style="color: #800080;">$arr2</span><span style="color: #000000;">);</span><span style="color: #008080;">33</span> <span style="color: #008080;">34</span> <span style="color: #800080;">$res3</span> =<span style="color: #000000;"> [];</span><span style="color: #008080;">35</span> <span style="color: #008080;">array_walk</span>(<span style="color: #800080;">$arr3</span>, <span style="color: #0000ff;">function</span>(<span style="color: #800080;">$val</span>, <span style="color: #800080;">$key</span>) <span style="color: #0000ff;">use</span>(&<span style="color: #800080;">$res3</span><span style="color: #000000;">) {</span><span style="color: #008080;">36</span> <span style="color: #800080;">$res3</span>[<span style="color: #800080;">$key</span>] = <span style="color: #008080;">array_filter</span>(<span style="color: #800080;">$val</span>, <span style="color: #0000ff;">function</span>(<span style="color: #800080;">$v</span><span style="color: #000000;">) {</span><span style="color: #008080;">37</span> <span style="color: #0000ff;">return</span> !<span style="color: #008080;">in_array</span>(6, <span style="color: #800080;">$v</span><span style="color: #000000;">);</span><span style="color: #008080;">38</span> <span style="color: #000000;"> });</span><span style="color: #008080;">39</span> <span style="color: #000000;"> });</span><span style="color: #008080;">40</span> <span style="color: #008080;">41</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$res2</span><span style="color: #000000;">);</span><span style="color: #008080;">42</span> <span style="color: #008080;">var_dump</span>(<span style="color: #800080;">$res3</span>);