我尝试使用php从我自定义生成的数组中删除重复记录。但我得到相同的数组.Below是输入数组,过滤后需要数组。
INPUT ARRAY:
Array
(
[0] => Array
(
[CSG] => Array
(
[group_id] => 1111
[group_name] => GRP NAME 1
[status] => 1
)
[TFE] => Array
(
[track_fb_export_id] =>
)
)
[1] => Array
(
[CSG] => Array
(
[group_id] => 2222
[group_name] => GRP NAME 2
[status] => 1
)
[TFE] => Array
(
[track_fb_export_id] =>
)
)
[2] => Array
(
[CSG] => Array
(
[group_id] => 2222
[group_name] => GRP NAME 2
[status] => 1
)
[TFE] => Array
(
[track_fb_export_id] =>
)
)
)
预期结果是什么:
Array
(
[0] => Array
(
[CSG] => Array
(
[group_id] => 1111
[group_name] => GRP NAME 1
[status] => 1
)
[TFE] => Array
(
[track_fb_export_id] =>
)
)
[1] => Array
(
[CSG] => Array
(
[group_id] => 2222
[group_name] => GRP NAME 2
[status] => 1
)
[TFE] => Array
(
[track_fb_export_id] =>
)
)
)
带有group_id的GRP NAME 2是重复的,因此想要从数组中删除重复的组。
我试过的代码:
$unique = array_map("unserialize", array_unique(array_map("serialize", $input)));
感谢任何帮助。
答案 0 :(得分:0)
这对你有用
//data array
$data = array();
//group id collector
$group_id = array();
//the index won't change now
foreach($input as $key=>$value)
{
if(!in_array($value['CSG']['group_id'], $group_id))
{
$group_id[] = $value['CSG']['group_id'];
$data[] = $value;
}
}
//check the data
print_r($data);
答案 1 :(得分:0)
几步:
group_name
:function cmp($a, $b) {
$sa = $a['CSG']['group_name'];
$sb = $b['CSG']['group_name'];
$compare = strcmp($a, $b);
if($compare > 0)
return 1;
else if($compare < 0)
return -1;
else
return 0;
}
uasort($array, 'cmp');
函数uasort采用一个数组和一个函数来调用数组。
$newArray = array();
$lastArray = null;
foreach($a as $array) {
if($lastArray == null || $a['CSG']['group_name'] != $lastArray['CSG']['group_name']) {
$lastArray = $a;
$newArray[] = $a;
}
}