Home > Article > Daily Programming > How to delete duplicate elements in a two-dimensional array in PHP? (Pictures + Videos)
This article mainly introduces to you the specific implementation method of PHP to delete duplicate elements in a two-dimensional array!
In the previous article [How to delete duplicate elements in an array in PHP? ] has introduced to you the method of deleting ordinary one-dimensional arrays in PHP. Friends who need it can refer to it first.
Simply put, a two-dimensional array is an array of arrays. You need two indexes to select elements.
Below we will introduce in detail the method of deleting duplicate elements in a two-dimensional array through specific code examples!
<?php /** * PHP删除二维数组中的重复元素 */ // 对二维数组进行去重复 function more_array_unique($arr) { //先把二维数组中的内层数组的键值记录在在一维数组中 foreach ($arr[0] as $k => $v) { $arr_inner_key[] = $k; } foreach ($arr as $k => $v) { //降维 用implode()也行 $v = join(",", $v); //保留原来的键值 $temp[]即为不保留原来键值 $temp[$k] = $v; } //去重:去掉重复的元素 $temp = array_unique($temp); foreach ($temp as $k => $v) { //拆分后的重组 如:Array( [0] => 张三 [1] => 18 ) $a = explode(",", $v); //将原来的键与值重新合并 $arr_after[$k] = array_combine($arr_inner_key, $a); } return $arr_after; } $arr = [ ['name' => '张三', 'age' => 18], ['name' => 'Tom', 'age' => 19], ['name' => 'Tom', 'age' => 19], ['name' => '张三', 'age' => 50], ['name' => '王小二', 'age' => 30], ]; echo "<pre class="brush:php;toolbar:false">"; print_r(more_array_unique($arr));
In this code, we define a two-dimensional array of $arr, which has two pairs of repeated elements, "Zhang San" and "Tom", but their ages are different.
Everyone should know that the array_unique() function only applies to one-dimensional arrays, so for two-dimensional arrays we need to define a function method ourselves, but the array_unique() function can also be used.
As in the above code example, we defined the more_array_unique method to delete duplicate elements in a two-dimensional array.
Note:
join function is an alias of this function: implode().
array_combine function: means to create an array, using the value of one array as its key name and the value of another array as its value
array_unique function: remove the array Duplicate values in .
Finally accessed through the browser, the result of deleting the duplicate elements of the two-dimensional array is as follows:
As shown in the figure, we use the customized more_array_unique method, Remove completely duplicate elements from a two-dimensional array while retaining useful information.
This article is about the specific method of PHP deleting duplicate elements in a two-dimensional array. I hope it will be helpful to friends in need!
If you want to learn more about PHP, you can follow the PHP Chinese website PHP Video Tutorial, everyone is welcome to refer to and learn!
The above is the detailed content of How to delete duplicate elements in a two-dimensional array in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!