PHP-dimensional array of filter elements with two keys are the same data

Can be made directly to 'maintaining a constant source array keys during execution name ', ' City data two fields' by array_unique removal function after repetitive elements Next array_diff_assoc than the difference before and after modification is stored in the new array, finally using foreach query two field screening situation can be the result. Man of few words said, directly on the source code:

<?php
$provinces = [
[
'id' => 1,
'name' => '张三',
'city' => '湖南'
],[
'id' => 2,
'name' => '李四',
'city' => '河北'
],[
'id' => 3,
'name' => '张三',
'city' => '湖北'
],[
'id' => 4,
'name' => '王五',
'city' => '北京'
],[
'id' => 5,
'name' => '王五',
'city' => '北京'
],
];

list($arr1, $arr2) = [array_column($provinces, 'name'), array_column($provinces, 'city')];
list($arr1, $arr2) = [array_diff_assoc($arr1, array_unique($arr1)), array_diff_assoc($arr2, array_unique($arr2))];
foreach($arr1 as $k => $v) 
if(array_key_exists($k, $arr2))
$result[] = $v; //$result[] = $provinces[$k];
var_dump($result);
Released three original articles · won praise 0 · Views 767

Guess you like

Origin blog.csdn.net/xuanziran/article/details/104872331