Ray
Ray

Reputation: 884

Combine Arrays based on certain keys

I am processing a csv and need to combine an array if two of the three keys match. For instance, take the following example:

<?php

$data = [
    [
        'id' => 1,
        'player' => 'John',
        'sport' => 'Football'
    ],
    [
        'id' => 2,
        'player' => 'John',
        'sport' => 'Football'
    ],
    [
        'id' => 3,
        'player' => 'Mike',
        'sport' => 'Soccer'
    ]
];

Below is the desired result.

$wanted = [
    [
        'id' => [1,2],
        'player' => 'John',
        'sport' => 'Football',
    ],
    [
        'id' => [3],
        'player' => 'Mike',
        'sport' => 'Soccer',
    ],
];

What would be the optimal way to achieve this?

Upvotes: 0

Views: 40

Answers (2)

Ganesh Kandu
Ganesh Kandu

Reputation: 621

check this out

<?php

$data = [
    [
        'id' => 1,
        'player' => 'John',
        'sport' => 'Football'
    ],
    [
        'id' => 2,
        'player' => 'John',
        'sport' => 'Football'
    ],
    [
        'id' => 3,
        'player' => 'John',
        'sport' => 'soccer'
    ],
    [
        'id' => 4,
        'player' => 'John',
        'sport' => 'soccer'
    ],
    [
        'id' => 5,
        'player' => 'Mike',
        'sport' => 'Soccer'
    ]
];

function combine($input){
    $output = [];
    foreach($input as $subarray){
        $output[$subarray['player'].$subarray['sport']]['id'][] = $subarray['id'];
        $output[$subarray['player'].$subarray['sport']]['player'] = $subarray['player'];
        $output[$subarray['player'].$subarray['sport']]['sport'] = $subarray['sport'];
    }
    return array_filter(array_values($output));
}

print_r(combine($data));

output

Array
(
    [0] => Array
        (
            [id] => Array
                (
                    [0] => 1
                    [1] => 2
                )

            [player] => John
            [sport] => Football
        )

    [1] => Array
        (
            [id] => Array
                (
                    [0] => 3
                    [1] => 4
                )

            [player] => John
            [sport] => soccer
        )

    [2] => Array
        (
            [id] => Array
                (
                    [0] => 5
                )

            [player] => Mike
            [sport] => Soccer
        )

)

Upvotes: 1

Ganesh Kandu
Ganesh Kandu

Reputation: 621

check this out

<?php

$data = [
    [
        'id' => 1,
        'player' => 'John',
        'sport' => 'Football'
    ],
    [
        'id' => 2,
        'player' => 'John',
        'sport' => 'Football'
    ],
    [
        'id' => 3,
        'player' => 'Mike',
        'sport' => 'Soccer'
    ]
];

function combine($input){
    $output = [];
    foreach($input as $subarray){
        $output[$subarray['player']]['id'][] = $subarray['id'];
        $output[$subarray['player']]['player'] = $subarray['player'];
        $output[$subarray['player']]['sport'] = $subarray['sport'];
    }
    return array_values($output);
}

print_r(combine($data));

output

Array
(
    [0] => Array
        (
            [id] => Array
                (
                    [0] => 1
                    [1] => 2
                )

            [player] => John
            [sport] => Football
        )

    [1] => Array
        (
            [id] => Array
                (
                    [0] => 3
                )

            [player] => Mike
            [sport] => Soccer
        )

)

Upvotes: 2

Related Questions