Home > Article > Backend Development > Let’s talk about the method of looping and deduplicating one-dimensional arrays in PHP
In PHP, array is a very important data type, which can store a set of related data. Sometimes we need to filter out unique elements from a one-dimensional array. In this case, loop deduplication is a good solution. This article will introduce several methods of looping and deduplicating one-dimensional arrays in PHP.
Method 1: Use array_unique() function
PHP built-in function array_unique() can be used to remove duplicate elements from an array. It returns a new array in which each element appears exactly once.
Sample code:
$original_arr = array("php", "java", "php", "python", "c++", "java"); $unique_arr = array_unique($original_arr); print_r($unique_arr);
Output result:
Array ( [0] => php [1] => java [3] => python [4] => c++ )
Method 2: Using foreach loop
In PHP, we can use foreach loop to traverse the array and Filter out unique elements. The specific steps are: determine in the loop whether each element in the array already exists in the result array, and if it does not exist, add it to the result array.
Sample code:
$original_arr = array("php", "java", "php", "python", "c++", "java"); $unique_arr = array(); foreach ($original_arr as $value) { if (!in_array($value, $unique_arr)) { $unique_arr[] = $value; } } print_r($unique_arr);
Output result:
Array ( [0] => php [1] => java [3] => python [4] => c++ )
Method 3: Use for loop
If the length of the array is known, we can use for loop Iterate through the array and filter out unique elements. The specific steps are similar to method two.
Sample code:
$original_arr = array("php", "java", "php", "python", "c++", "java"); $unique_arr = array(); for ($i = 0; $i < count($original_arr); $i++) { if (!in_array($original_arr[$i], $unique_arr)) { $unique_arr[] = $original_arr[$i]; } } print_r($unique_arr);
Output result:
Array ( [0] => php [1] => java [3] => python [4] => c++ )
Method 4: Using array_keys() and array_values() functions
In PHP, we can Use the array_keys() function to obtain the key array of an array, and then use the array_values() function to obtain the corresponding array of values. Using these two functions, we can easily remove duplicate elements from the array.
Sample code:
$original_arr = array("php", "java", "php", "python", "c++", "java"); $unique_arr = array_values(array_unique($original_arr)); print_r($unique_arr);
Output result:
Array ( [0] => php [1] => java [2] => python [3] => c++ )
Summary:
This article introduces several commonly used one-dimensional array loop deduplication methods in PHP Methods include using the built-in function array_unique(), using a foreach loop, using a for loop, and using the array_keys() and array_values() functions. Just choose different methods according to actual needs. It should be noted that in the process of deduplication, the key values of the array are very important, do not ignore their existence.
The above is the detailed content of Let’s talk about the method of looping and deduplicating one-dimensional arrays in PHP. For more information, please follow other related articles on the PHP Chinese website!