How these three arrays are combined into an array, give a thought or code thank you

Source: Internet
Author: User

How do I add the values in multiple arrays?

Reply content:

How do I add the values in multiple arrays?

can be usedarray_reduce(數組, 處理函數, 初始值)

array_reduce-iteratively to simplify the array to a single value with a callback function


  
    1 ]    ],    [        [ 'count' => 2 ],        [ 'count' => 67 ],    ]    ,    [        [ 'count' => 3 ],        [ 'count' => 2 ],    ],];$sum = array_reduce($arr, function($currentSumOuter, $itemOuter) {        // 這裡是各組中的 count 總和    // 返回結構是 [ 'count' => 總和 ]    $sumInner = array_reduce($itemOuter, function($currentSumInner, $itemInner) {        return [            'count' => $itemInner['count'] + $currentSumInner['count']        ];    }, 0);        // 這裡再按照原先結構,把各組總和再次總和    return [        [             'count' => $currentSumOuter[0]['count'] + $sumInner['count']        ]    ];}, 0);echo '
';print_r($sum);echo '
';
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.