How to remove duplicate values ​​from a three-dimensional array in PHP (three methods)

PHPz
Release: 2023-04-12 11:33:30
Original
841 people have browsed it

PHP is a very popular programming language that is widely used for web development. In the process of writing PHP programs, you often encounter the need to process multi-dimensional arrays (also called multiple arrays). Sometimes, there will be duplicate elements in these arrays, which will cause some trouble for our program. This article will introduce how to remove duplicate values ​​from a three-dimensional array in PHP.

  1. Use loop traversal method

The most common way to remove duplicate values ​​​​from an array is to use a loop to traverse each element in the array. The specific steps are as follows:

  • Traverse each element in the array
  • Check whether the element already exists in a new array
  • If it does not exist, then The element is added to the new array
  • If it exists, the element is skipped

The following is a sample code that uses the loop traversal method to remove duplicate values ​​in a three-dimensional array:

function array_unique_multidim($array){
    $unique_array = array();
    foreach($array as $key => $value){
        if(!in_array($value, $unique_array)){ 
            $unique_array[$key] = $value;
        }
    }
    return $unique_array;
}
Copy after login

This code will return a new array with no duplicate elements. This method can handle multiple arrays recursively. However, this method is slower and is suitable for processing small arrays.

  1. Use the array_unique() function

PHP’s built-in array_unique() function can remove duplicate values ​​​​from the array, but this function can only be used for one-dimensional arrays. For multi-dimensional arrays, we need to convert them to one-dimensional arrays first, and then use this function to remove duplicates.

The following is a sample code that uses the array_unique() function to remove duplicate values ​​from an array:

function array_unique_multidim($array){
    $flatten_array = array_reduce($array, 'array_merge', array());
    $unique_array = array_unique($flatten_array);
    foreach($unique_array as $key => $value){
        if(is_array($value)){ 
            $unique_array[$key] = array_unique_multidim($value);
        }
    }
    return $unique_array;
}
Copy after login

This code will return a new array with no duplicate elements. This method is faster and simpler, suitable for processing large arrays.

  1. Specify a unique index

If we have an index key that represents a unique value in the array, we can use that key to remove duplicate values. The specific steps are as follows:

  • Traverse each element in the array
  • Check whether the unique key of the element already exists in a new array
  • If it does not exist , then add the element to the new array
  • If it exists, skip the element

The following is a sample code to remove duplicate values ​​from an array using a unique index key:

function array_unique_multidim($array, $key){
    $unique_array = array();
    foreach($array as $value){
        if(!array_key_exists($value[$key], $unique_array)){
            $unique_array[$value[$key]] = $value;
        }
    }
    return $unique_array;
}
Copy after login

This code will return a new array with no duplicate elements. This method is faster and suitable for processing arrays that need to retain specific keys.

To sum up, these three methods each have their own advantages and disadvantages and can be chosen according to different needs. For smaller arrays, the loop traversal method is a better choice, while for large arrays, you can use the array_unique() function or the method of specifying a unique index key.

The above is the detailed content of How to remove duplicate values ​​from a three-dimensional array in PHP (three methods). For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template