[英]php 2d array sorting with unique
I've got this array: 我有这个数组:
Array
(
[0] => Array
(
[id] => new@particip.pl
[challs] =>
)
[1] => Array
(
[id] => new@email.pl
[challs] => 551
)
[2] => Array
(
[id] => new@email.pl
[challs] => 551
)
[3] => Array
(
[id] => new@email.pl
[challs] => 553
)
[4] => Array
(
[id] => new@email.pl
[challs] => 553
)
[5] => Array
(
[id] => info@mail.com
[challs] =>
)
)
How make this to array with unique email addresses and joined challs, for ex in this case: 在这种情况下,如何使它排列为具有唯一的电子邮件地址和加入的查尔斯,例如:
Array
(
[0] => Array
(
[id] => new@particip.pl
[challs] =>
)
[1] => Array
(
[id] => new@email.pl
[challs] => 551, 553
)
[2] => Array
(
[id] => info@email.com
[challs] =>
)
)
Can be it sorted, or I need to do forach inarray? 可以排序吗,还是我需要进行inarray? Someone had idea how to do it?
有人知道怎么做吗?
I guess something along the line of the following is what you want: 我想以下是您想要的:
$result = array();
foreach ($arrays as $array) {
if (! isset($result[$array['id']])) {
$result[$array['id']] = array(
'id' => $array['id'],
'challs' => $array['chall']
);
}
if (! in_array($result[$array['id']]['challs'], $array['chall'])) {
$result[$array['id']]['challs'] .= ', ' . $array['chall'];
}
}
$result = array_values($result);
$emailsSeen = array();
foreach($array as $key => &$item) {
if(in_array($item['id'], $emailsSeen) {
//concatenate 'challs' of repeated email to first occurence of email in $array
$array[array_search($item['id'], $emailsSeen)]['challs'] .= ', ' . $item['challs'];
//remove duplicate email item from $array
unset($array[$key']);
} else {
$emailsSeen[$key] = $item['id'];
}
}
//To reindex the array
$array = array_values($array);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.