Eindimensionales 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>
Zweidimensionales Array
1) Da der Wert eines bestimmten Schlüsselnamens nicht wiederholt werden kann, löschen Sie Duplikate
<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) Da die internen eindimensionalen Arrays nicht genau gleich sein können, werden doppelte Elemente gelöscht
<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>
Urheberrechtserklärung: Wissen kommt von den Menschen und wird genutzt vom Volk! Ein Nachdruck ist willkommen. Bitte fügen Sie am Anfang einen Link zu diesem Artikel hinzu. Der Artikel wird von Zeit zu Zeit aktualisiert!
Das Obige stellt die PHP-Array-Deduplizierung vor, einschließlich inhaltlicher Aspekte. Ich hoffe, dass es für Freunde hilfreich ist, die sich für PHP-Tutorials interessieren.