Merge/Sum/Add multi-dimensional array values with alpha keys
Results 1 to 5 of 5

Thread: Merge/Sum/Add multi-dimensional array values with alpha keys

  1. #1
    I see what you did there
    Join Date
    Sep 2004
    Location
    Albany, NY
    Posts
    1,901

    Question Merge/Sum/Add multi-dimensional array values with alpha keys

    Is there a clean way to accomplish the following without writing an ugly bunch of for loops and logic blocks?

    PHP Code:
    <?php

    //INPUT
    $aRaw = [
        [
    'in' => [1,2,3], 'out' =>[4,5,6]],
        [
    'in' => [7,8,9], 'out' =>[1,2,3]]
    ];

    //DESIRED OUTPUT
    $aSum = ['in' => [8,10,12], 'out' =>[5,7,9]];

    ?>
    $aN is a factor, so there may only be one set in $aRaw or there may be 5+

    Thank you =)
    ***If your problem has been solved, PLEASE click the RESOLVED LINK under "Thread Tools"***

    PHP Manual | Learn HTML | Validate HTML

  2. #2
    I see what you did there
    Join Date
    Sep 2004
    Location
    Albany, NY
    Posts
    1,901
    I mean, this works, but triple nested foreach loops makes my skin crawl AND throws undefined offsets where $sum is being filled:

    PHP Code:
    <?php
    //INPUT
    $aRaw = [
        [
    'in' => [1,2,3], 'out' =>[4,5,6]],
        [
    'in' => [7,8,9], 'out' =>[1,2,3]]
        ];

    $sum = [];
    foreach(
    $aRaw as $aSet) {
        foreach(
    $aSet as $k => $kv) {
            
    $i 0;
            foreach(
    $kv as $v) {
                
    $sum[$k][$i] += $v;
                
    $i++;
            }
        }
    }

    print_r($sum);
    ?>
    Code:
    Array
    (
        [in] => Array
            (
                [0] => 8
                [1] => 10
                [2] => 12
            )
    
        [out] => Array
            (
                [0] => 5
                [1] => 7
                [2] => 9
            )
    
    )
    Gotta be a better way
    Last edited by scrupul0us; 03-16-2017 at 10:15 AM.
    ***If your problem has been solved, PLEASE click the RESOLVED LINK under "Thread Tools"***

    PHP Manual | Learn HTML | Validate HTML

  3. #3
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,807
    So far everything else I think of ends up more complicated.
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  4. #4
    Senior Member Derokorian's Avatar
    Join Date
    Apr 2011
    Location
    Denver
    Posts
    2,261
    Change:
    PHP Code:
    // from this
    $sum = []; 

    // to this
    $sum = [array_fill(0,3,0), array_fill(0,3,0)]; 
    And that will remove the notices from it being undefined. Everything is going to involve nested loops unfortunately, even if you us array_map and friends, you're just moving the loop to inside the C function.
    Sadly, nobody codes for anyone on this forum. People taste your dishes and tell you what is missing, but they don't cook for you. ~anoopmail
    I'd rather be a comma, then a full stop.
    User Authentication in PHP with MySQLi - Don't forget to mark threads resolved - MySQL(i) warning

  5. #5
    I see what you did there
    Join Date
    Sep 2004
    Location
    Albany, NY
    Posts
    1,901
    Thanks guys... Added in @Derokorian's suggestion and it's workin'

    Was hoping for a more elegant way but needs vs wants
    ***If your problem has been solved, PLEASE click the RESOLVED LINK under "Thread Tools"***

    PHP Manual | Learn HTML | Validate HTML

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •