Skip to content
Advertisement

Get common values in a multidimension array

hope you’re doing well I have an multidimensional array whose output is given below

[888] => Array
    (
        [team_name] => freight
        [total] => 7707103.66482
    )

[887] => Array
    (
        [team_name] => freight
        [total] => 6954528.9139
    )

[890] => Array
    (
        [team_name] => assembly line
        [total] => 1953
    )

I want to get all the indexes in a separate array which have common team_name.

Advertisement

Answer

Maybe something like this could do the job:

$result = [];
foreach($yourArray as $ar) {
    if (isset($result[$ar['team_name]])) {
        $result[$ar['team_name']] += $ar['total'];
    } else {
        $result[$ar['team_name']] = $ar['total'];
    }
}

The $result-array should then look like this (not tested though!):

array(
    'freight' => 7707103.66482 + 6954528.9139,
    'assebly line' => 1953
) 

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement