首頁  >  文章  >  後端開發  >  php 陣列去重

php 陣列去重

WBOY
WBOY原創
2016-07-29 09:15:431087瀏覽

一維數組

<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教學有興趣的朋友有幫助。

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn