Home >Backend Development >PHP Tutorial >php array deduplication

php array deduplication

WBOY
WBOYOriginal
2016-07-29 09:15:431107browse

One-dimensional array

<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>

Two-dimensional array

1) Because the value of a certain key name cannot be repeated, delete duplicates

<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) Because the internal one-dimensional array cannot be exactly the same, delete duplicates

<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>

Copyright Statement: Knowledge comes from the people and is used by the people! Reprinting is welcome. Please attach a link to this article at the beginning. The article will be updated from time to time!

The above introduces PHP array deduplication, including aspects of content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn