1

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? Someone had idea how to do it?

Jan Turoň
  • 26,696
  • 21
  • 102
  • 153
user2213609
  • 73
  • 1
  • 8

2 Answers2

0

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);
Jan-Henk
  • 4,714
  • 1
  • 21
  • 38
  • close, but it's not it, I got: Array ( [new@particip.pl] => Array ( [id] => new@particip.pl [challs] => Array ( [0] => ) ) [new@id.pl] => Array ( [id] => new@id.pl [challs] => Array ( [0] => ) ) [info@cowonder.com] => Array ( [id] => info@cowonder.com [challs] => Array ( [0] => ) ) ) – user2213609 May 16 '13 at 21:36
  • What do you get? I believe the only difference is that the resulting array is indexed by the email addresses instead of 0, 1, 2, etc. Or is the problem that challs in the resulting array is an array instead of a string? – Jan-Henk May 16 '13 at 21:37
  • I updated my answer, the resulting array is now zero-indexed and chall is a string with the values separated by comma's. – Jan-Henk May 16 '13 at 21:59
0
$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);
km6zla
  • 4,634
  • 1
  • 26
  • 49