简体   繁体   中英

How to sum same field values in multidimentional array

I have to admit that understanding the tables are big challenge for me so please dont judge me so hard...

This is my array:

Array
(
    [names] => Array
        (
            [0] => Name1
            [1] => Name2
            [2] => Name1
        )

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

    [quantities] => Array
        (
            [0] => 255
            [1] => 2
            [2] => 467
        )

)

And i wish to sum " quantities " where names or ids are the same.

Example output should be:

Array
(
    [names] => Array
        (
            [0] => Name1
            [1] => Name2
        )

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

    [quantities] => Array
        (
            [0] => 722
            [1] => 2
        )

)

I know there is a function like " array_reduce " but don't know how to use it.

Thanks for help!

try this

    $result = [];
    foreach($array['ids'] as $key=>$val ){

        if(array_key_exists($val, $result)){

            $result[$val]['sum_quantity'] += $array['quantities'][$key];
        }
        else{
            $result[$val]['sum_quantity'] = $array['quantities'][$key];
            $result[$val]['name'] = $array['names'][$key];
            $result[$val]['id'] = $array['ids'][$key];
        }
    }

and output will be like this

    Array
    (
        [1] => Array //array key = id
        (
            ['name'] => Name1,
            ['sum_quantity'] => 722,
            ['id'] => 1
        )

        [2] => Array
        (
            ['name'] => Name2,
            ['sum_quantity'] => 2,
            ['id'] => 2
        )
)

You can do this way :

    $testArray['names'][0]='name1';
    $testArray['names'][1]='name2';
    $testArray['names'][2]='name1';

    $testArray['ids'][0]=1;
    $testArray['ids'][1]=2;
    $testArray['ids'][2]=1;

    $testArray['quantities'][0]=255;
    $testArray['quantities'][1]=2;
    $testArray['quantities'][2]=467;

    echo "<pre>";

    print_r($testArray);

    $unqArray['names']=array();
    $unqArray['ids']=array();
    $unqArray['quantities']=array();

    foreach($testArray['ids'] as $key=>$value)
    {
        if(!in_array($value,$unqArray['ids']))
        {
            $unqArray['names'][]=$testArray['names'][$key];
            $unqArray['ids'][]=$testArray['ids'][$key];
            $quantity=0;
            foreach($testArray['ids'] as $keyId=>$valueId)
            {
                if($valueId==$value)
                {
                    $quantity+=$testArray['quantities'][$keyId];
                }
            }
            $unqArray['quantities'][]=$quantity;
        }
    }

    print_r($unqArray);

    echo "</pre>";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM