需要将多维阵列合并为单个数组,从而将重复值以用户名和用户朋友详细信息为用户名
elemine。
Array
(
[Nishanth] => Array
(
[0] => Array
(
[ID] => 3
[username] => IronMan
)
[1] => Array
(
[ID] => 5
[username] => SpiderMan
)
[2] => Array
(
[ID] => 8
[username] => AntMan
)
[3] => Array
(
[ID] => 10
[username] => BatMan
)
[4] => Array
(
[ID] => 11
[username] => SuperMan
)
)
[IronMan] => Array
(
[0] => Array
(
[ID] => 1
[username] => Nishanth
)
[2] => Array
(
[ID] => 5
[username] => SpiderMan
)
)
[SpiderMan] => Array
(
[0] => Array
(
[ID] => 1
[username] => Nishanth
)
[1] => Array
(
[ID] => 3
[username] => IronMan
)
[2] => Array
(
[ID] => 8
[username] => AntMan
)
[3] => Array
(
[ID] => 10
[username] => BatMan
)
[4] => Array
(
[ID] => 14
[username] => Hulk
)
)
[AntMan] => Array
(
[0] => Array
(
[ID] => 1
[username] => Nishanth
)
[1] => Array
(
[ID] => 10
[username] => BatMan
)
)
[BatMan] => Array
(
[0] => Array
(
[ID] => 1
[username] => Nishanth
)
[1] => Array
(
[ID] => 5
[username] => SpiderMan
)
[2] => Array
(
[ID] => 8
[username] => AntMan
)
[3] => Array
(
[ID] => 11
[username] => SuperMan
)
)
[SuperMan] => Array
(
[0] => Array
(
[ID] => 1
[username] => Nishanth
)
[1] => Array
(
[ID] => 10
[username] => BatMan
)
)
[Hulk] => Array
(
[0] => Array
(
[ID] => 5
[username] => SpiderMan
)
)
)
我应该如何合并上述数组,如下所示
预期结果:
[MergedUser] => Array
(
[0] => Array
(
[ID] => 3
[username] => IronMan
)
[1] => Array
(
[ID] => 5
[username] => SpiderMan
)
[2] => Array
(
[ID] => 8
[username] => AntMan
)
[3] => Array
(
[ID] => 10
[username] => BatMan
)
[4] => Array
(
[ID] => 11
[username] => SuperMan
)
[5] => Array
(
[ID] => 14
[username] => Hulk
)
)
使用array_unique()
将按原样打印。我们应该如何实现这一目标?
<?php
$people =
[
[
[
'id'=>3,
'name'=>'George'
],
[
'id'=>5,
'name'=>'Ringo'
]
],
[
[
'id'=>3,
'name'=>'George'
],
[
'id'=>7,
'name'=>'Paul'
]
],
[
[
'id'=> 9,
'name'=> 'John'
]
]
];
$peeps = array_merge(...$people);
$peeps = array_column($peeps, null, 'id');
var_export($peeps);
输出:
array (
3 =>
array (
'id' => 3,
'name' => 'George',
),
5 =>
array (
'id' => 5,
'name' => 'Ringo',
),
7 =>
array (
'id' => 7,
'name' => 'Paul',
),
9 =>
array (
'id' => 9,
'name' => 'John',
),
)
array_column,将通过使用ID作为最终数组的键来滤除重复。
感谢Alive死于您的原始格式。由于阵列解开包装键失败,因此可以首先通过array_values的调用来修复,因此在您的情况下:
$output = array_merge(...array_values($array));
$output = array_column($output, null, 'ID');
您可以使用 array_map
和 foreach' to remove duplicated from the existing
array`
$res['MergedUser'] = [];
array_map(function($v) use (&$res){
foreach($v as $value){
if(!array_key_exists($value['ID'], $res['MergedUser']))
$res['MergedUser'][$value['ID']] = $value;
}
}, $arr);
实时演示
您可以使用功能实现相同的功能,请参阅Inline Doc。
function unique_multidim_array($array, $key)
{
$temp_array = [];
$i = 0;
$key_array = [];
foreach ($array as $val) {
foreach ($val as $key1 => $value1) {
if (!in_array($value1[$key], $key_array)) { // will check if already in array
$key_array[$i] = $value1[$key]; // once added then wont be added again
$temp_array[$i] = $value1; // result array
}
$i++;
}
}
$ret['MergedUser'] = $temp_array;
return $ret;
}
$temp = unique_multidim_array($array, "ID"); // unique by which key
工作演示。
您可以按照以下来生成唯一的数组。
array_unique($YOUR_ARRAY_VARIABLE, SORT_REGULAR);
这样,您的数组中只有独特的值而不是重复。
<?php
// define array
$a = array(1, 5, 2, 5, 1, 3, 2, 4, 5);
// print original array
echo "Original Array : n";
print_r($a);
// remove duplicate values by using
// flipping keys and values
$a = array_flip($a);
// restore the array elements by again
// flipping keys and values.
$a = array_flip($a);
// re-order the array keys
$a= array_values($a);
// print updated array
echo "nUpdated Array : n ";
print_r($a);
?>
我希望此链接对您有帮助。