需要将多维数组合并为单个数组,从而消除以键为用户名和值作为用户朋友详细信息的重复值
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()
将按原样打印。我们应该如何实现这一目标?
答案 0 :(得分:2)
<?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作为最终数组的键来过滤出重复项。
感谢“活死人”,因为这会破坏您的原始格式。由于数组解压缩失败并带有字符串键,因此可以先通过调用array_values来解决此问题,所以在您的情况下:
$output = array_merge(...array_values($array));
$output = array_column($output, null, 'ID');
答案 1 :(得分:2)
您可以使用函数来实现相同功能,请参阅内联文档。
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
工作demo。
答案 2 :(得分:1)
将foreach()
与array_values()
一起使用:
$final_array = array();
foreach($array as $arr){
foreach($arr as $a){
$final_array[$a['ID']]['ID']= $a['ID'];
$final_array[$a['ID']]['username']= $a['username'];
}
}
$final_array = array_values($final_array);
print_r($final_array);
参考文献:-
答案 3 :(得分:1)
您可以使用array_map
和foreach' to remove duplicated from the existing
数组`
$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);
答案 4 :(得分:1)
您可以执行以下操作以生成唯一的数组。
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);
?>
我希望this link能为您提供帮助。