In PHP programming, array is a very important data type that allows us to store multiple values and access them through keys. A two-dimensional array is a one-dimensional array with an additional dimension, allowing us to store more complex data structures. However, in the process of using two-dimensional arrays, we often encounter a problem, that is, how to detect whether there are duplicate values in the two-dimensional array. This article will introduce in detail how to determine whether there are duplicate values in a two-dimensional array.
1. What is a two-dimensional array?
Before introducing how to determine whether there are duplicate values in a two-dimensional array, we need to first understand what a two-dimensional array is. As briefly introduced above, a two-dimensional array adds another dimension to a one-dimensional array. That is to say, each element is no longer a single value, but an array. That is, each element is an array containing multiple values, which can be strings, numbers, booleans, etc. of any type.
The following is a simple two-dimensional array example:
$cars = array( array("Volvo",22,18), array("BMW",15,13), array("Saab",5,2), array("Land Rover",17,15) );
In this example, we define a two-dimensional array $cars, which contains four array elements, each element Contains three values. We can access the first value of the first element in the $cars array through the following code:
echo $cars[0][0]; // 输出 Volvo
2. Determine whether there are duplicate values in the two-dimensional array
In actual development, We often need to determine whether there are duplicate values in a two-dimensional array. There are many ways to solve this problem, two of which we introduce here.
1. Use foreach loop nesting
Use foreach loop nesting to traverse each element in the two-dimensional array and compare their values in turn. The specific implementation steps are as follows:
① Define an empty array $result to save elements without repeated values;
② Use a foreach loop to traverse the original two-dimensional array $source and take them out in sequence Each element;
③ Use the foreach loop again in the inner loop to traverse the $result array, and compare whether the current element is the same as each element already saved in the $result array;
④ If they are the same, jump out of the inner loop and continue traversing the next element; if they are different, save the current element to the $result array and exit the loop.
The specific code is as follows:
$source = array( // 原始的二维数组 array('name'=>'Tom','age'=>18), array('name'=>'Jack','age'=>23), array('name'=>'Mary','age'=>21), array('name'=>'Tom','age'=>20), array('name'=>'Jim','age'=>32) ); $result = array(); // 存放没有重复值的元素 foreach ($source as $value) { $flag = true; // 标记是否存在重复值 foreach ($result as $v) { if ($value['name'] == $v['name']) { $flag = false; break; } } if ($flag) { $result[] = $value; } } print_r($result); // 输出结果:Array ( [0] => Array ( [name] => Tom [age] => 18 ) [1] => Array ( [name] => Jack [age] => 23 ) [2] => Array ( [name] => Mary [age] => 21 ) [4] => Array ( [name] => Jim [age] => 32 ) )
In the above code, we first define an empty array $result, and then use a two-layer foreach loop to traverse the original two-dimensional array $source . If it is found that the name value of the current element already exists in the $result array, it will directly exit the current loop and continue to traverse the next element; if there are no duplicate values, the current element will be saved in the $result array.
2. Use PHP's built-in array function array_unique
PHP has a built-in array function array_unique, which can be used to remove duplicate values in an array. This function is very simple, you only need to pass in an array as a parameter, and it will return a new array in which all duplicate values have been removed. If you want to use the array_unique function to remove duplicate values in a two-dimensional array, we can use the following code:
$source = array( // 原始的二维数组 array('name'=>'Tom','age'=>18), array('name'=>'Jack','age'=>23), array('name'=>'Mary','age'=>21), array('name'=>'Tom','age'=>20), array('name'=>'Jim','age'=>32) ); $name_arr = array_column($source, 'name'); // 获取所有name值的数组 $unique_arr = array_unique($name_arr); // 去重 $result = array(); // 存放没有重复值的元素 foreach ($source as $value) { if (in_array($value['name'], $unique_arr)) { $result[] = $value; } } print_r($result); // 输出结果:Array ( [0] => Array ( [name] => Tom [age] => 18 ) [1] => Array ( [name] => Jack [age] => 23 ) [2] => Array ( [name] => Mary [age] => 21 ) [4] => Array ( [name] => Jim [age] => 32 ) )
In the above code, we first use the array_column function to obtain the name values in all two-dimensional arrays, and then Duplicate values are removed using the array_unique function. Next, we loop through the original two-dimensional array again, and if the name value of the current element exists in $unique_arr, we save the current element to the $result array.
Summary:
The above are two methods to determine whether there are duplicate values in a PHP two-dimensional array. Through these two methods, you can quickly determine whether there are duplicate elements in a two-dimensional array. Among them, the method of using PHP's built-in function array_unique function is simpler, but it also consumes more computing resources, while the method of using two levels of foreach loop nesting is more flexible and efficient. Developers can choose the method that suits them based on their specific needs.
The above is the detailed content of How to check whether there are duplicate values in a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!