Rumah >pembangunan bahagian belakang >tutorial php >php 数组去重
一维数组
<code><span>$aa</span>=<span>array</span>(<span>"apple"</span>,<span>"banana"</span>,<span>"pear"</span>,<span>"apple"</span>,<span>"wail"</span>,<span>"watermalon"</span>); <span>$bb</span>=array_unique(<span>$aa</span>); print_r(<span>$bb</span>);<span>//Array ( [0] => apple [1] => banana [2] => pear [4] => wail [5] => watermalon</span></code>
二维数组
1)因为某一键名的值不能重复,删除重复项
<code><span>$aa</span> = <span>array</span>( <span>array</span>(<span>'id'</span> => <span>123</span>, <span>'name'</span> => <span>'张三'</span>), <span>array</span>(<span>'id'</span> => <span>123</span>, <span>'name'</span> => <span>'李四'</span>), <span>array</span>(<span>'id'</span> => <span>124</span>, <span>'name'</span> => <span>'王五'</span>), <span>array</span>(<span>'id'</span> => <span>125</span>, <span>'name'</span> => <span>'赵六'</span>), <span>array</span>(<span>'id'</span> => <span>126</span>, <span>'name'</span> => <span>'赵六'</span>) ); <span>//需求,id 不能重复</span><span><span>function</span><span>assoc_unique</span><span>(<span>$arr</span>, <span>$key</span>)</span>{</span><span>$tmp_arr</span> = <span>array</span>(); <span>foreach</span>(<span>$arr</span><span>as</span><span>$k</span> => <span>$v</span>){ <span>//搜索$v[$key]是否在$tmp_arr数组中存在,若存在返回true</span><span>if</span>(in_array(<span>$v</span>[<span>$key</span>], <span>$tmp_arr</span>)){ <span>unset</span>(<span>$arr</span>[<span>$k</span>]); }<span>else</span> { <span>$tmp_arr</span>[] = <span>$v</span>[<span>$key</span>]; } } sort(<span>$arr</span>); <span>//sort函数对数组进行排序</span><span>return</span><span>$arr</span>; } <span>$key</span> = <span>'id'</span>; assoc_unique(&<span>$aa</span>, <span>$key</span>); print_r(<span>$aa</span>); <span>//Array ( [0] => Array ( [id] => 123 [name] => 张三 ) [1] => Array ( [id] => 124 [name] => 王五 ) [2] => Array ( [id] => 125 [name] => 赵六 ) [3] => Array ( [id] => 126 [name] => 赵六 ) )</span></code>
2)因内部的一维数组不能完全相同,而删除重复项
<code><span>$aa</span> = <span>array</span>( <span>array</span>(<span>'id'</span> => <span>123</span>, <span>'name'</span> => <span>'张三'</span>), <span>array</span>(<span>'id'</span> => <span>123</span>, <span>'name'</span> => <span>'李四'</span>), <span>array</span>(<span>'id'</span> => <span>124</span>, <span>'name'</span> => <span>'王五'</span>), <span>array</span>(<span>'id'</span> => <span>123</span>, <span>'name'</span> => <span>'李四'</span>), <span>array</span>(<span>'id'</span> => <span>126</span>, <span>'name'</span> => <span>'赵六'</span>) ); <span><span>function</span><span>array_unique_fb</span><span>(<span>$array2D</span>)</span>{</span><span>foreach</span> (<span>$array2D</span><span>as</span><span>$v</span>){ <span>$v</span> = join(<span>","</span>,<span>$v</span>); <span>//降维,也可以用implode,将一维数组转换为用逗号连接的字符串</span><span>$temp</span>[] = <span>$v</span>; } <span>$temp</span> = array_unique(<span>$temp</span>); <span>//去掉重复的字符串,也就是重复的一维数组</span><span>foreach</span> (<span>$temp</span><span>as</span><span>$k</span> => <span>$v</span>){ <span>$temp</span>[<span>$k</span>] = explode(<span>","</span>,<span>$v</span>); <span>//再将拆开的数组重新组装</span> } <span>return</span><span>$temp</span>; } <span>$bb</span>=array_unique_fb(<span>$aa</span>); print_r(<span>$bb</span>); <span>//Array ( [0] => Array ( [0] => 123 [1] => 张三 ) [1] => Array ( [0] => 123 [1] => 李四 ) [2] => Array ( [0] => 124 [1] => 王五 ) [4] => Array ( [0] => 126 [1] => 赵六 ) ) </span></code>
版权声明:知识取之于民,用之于民!欢迎转载,转载请开头附上本文链接,不定期更新文章!
以上就介绍了php 数组去重,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。