Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to remove duplicates in php arrays

Shulou Source: shulou.com Published: 2022-05-31 10:57:25 10月13日 Update

This article mainly explains "how to duplicate the php array". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to duplicate the php array.

1. Description

The arrray_unique function is used to remove duplicates from an array, and if more than two array values are the same, only the first value is retained.

The php5.2.9 version adds array_unique 's support for multi-dimensional groups, and the sort_flags parameter needs to be set to deal with multi-dimensional groups.

2. Grammar

Array_unique (array)

3. Parameters

Array

Sortingtype

4. Return value

Returns the filtered array.

5. Examples

One-dimensional array deduplicates:

Array_unique (array) $arr = array_unique ($classify)

Two-dimensional array deduplication:

Function assoc_unique ($arr, $key) {$tmp_arr = array (); foreach ($arr as $k = > $v) {if (in_array ($v [$key], $tmp_arr)) {unset ($arr [$k]);} else {$tmp_arr [] = $v [$key];} sort ($arr); return $arr } $aa = Array (Array (0 = > 1pint 1 = >'c'), Array (0 = > 2pint 1 = >'c'), Array (0 = > 3pint 1 = >'q'), Array (0 = > 1pint 1 = >'B'), Array (0 = > 2pint 1 = >'d'), Array (0 = > 3pint 1 = >'d'), Array (0 = > 4pm 1 = >'a'), Array (0 = > 6pm 1 = >'B'), Array (0 = > 6prima 1 = >'d')) # assoc_unique (array, de-health value); echo ""; print_r (assoc_unique ($aa,0))

When we use the array, we will find some duplicate elements, if we do not remove them, it will affect the subsequent use.

At this point, I believe you have a deeper understanding of "how to duplicate the php array". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Array content parameter multidimensional learning wrong practical deeper same two element interest function example practical simple operation method time more Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Shulou Information macOS vpn Docker