计算多维数组元素

问题描述 投票:0回答:2
Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [partyplan_select_products] => 1
                    [product_name] => Books
                    [partyplan_amazonurl_product] => http://www.flipkart.com
                    [partyplan_product_image_id] => 18320
                    [partyplan_product_image] => http://local.enjoius/wp-content/uploads/2016/11/acktobwufyhopfql.jpg
                    [partyplan_min_product_price] => 25.00
                    [partyplan_max_product_price] => 30.00
                    [partyplan_product_qty] => 0
                )

            [1] => Array
                (
                    [partyplan_select_products] => 1
                    [product_name] => Wallpaers
                    [partyplan_amazonurl_product] => http://www.Amazon.com
                    [partyplan_product_image_id] => 18351
                    [partyplan_product_image] => http://local.enjoius/wp-content/uploads/2016/11/20161114015751-3D-Nature-Wallpapers-HD-2.jpg
                    [partyplan_min_product_price] => 10.00
                    [partyplan_max_product_price] => 50.00
                    [partyplan_product_qty] => 100
                )

            [2] => Array
                (
                    [partyplan_select_products] => 1
                    [product_name] => Disco Lights
                    [partyplan_amazonurl_product] => http://www.google.com
                    [partyplan_product_image_id] => 18328
                    [partyplan_product_image] => http://local.enjoius/wp-content/uploads/2016/11/relwwzfpaupcvhlc.jpg
                    [partyplan_min_product_price] => 85.00
                    [partyplan_max_product_price] => 100.00
                    [partyplan_product_qty] => 75
                )

            [5] => Array
                (
                    [partyplan_select_products] => 1
                    [product_name] => Table
                    [partyplan_amazonurl_product] => http://www.google.com
                    [partyplan_product_image_id] => 18416
                    [partyplan_product_image] => http://local.enjoius/wp-content/uploads/2016/11/blog_img.jpg
                    [partyplan_min_product_price] => 150.00
                    [partyplan_max_product_price] => 500.00
                    [partyplan_product_qty] => 15
                )

        )

    [2] => Array
        (
            [3] => Array
                (
                    [partyplan_select_products] => 2
                    [product_name] => Waiters
                    [partyplan_amazonurl_product] => http://www.google.com
                    [partyplan_product_image_id] => 18373
                    [partyplan_product_image] => http://local.enjoius/wp-content/uploads/2016/11/20161123011316-8454547519_f8116520e1_b.jpg
                    [partyplan_min_product_price] => 150.00
                    [partyplan_max_product_price] => 500.00
                    [partyplan_product_qty] => 25
                )

            [4] => Array
                (
                    [partyplan_select_products] => 2
                    [product_name] => sadasdfs
                    [partyplan_amazonurl_product] => http://www.google.com
                    [partyplan_product_image_id] => 18362
                    [partyplan_product_image] => http://local.enjoius/wp-content/uploads/2016/11/20161116041623-8215602321_69d9939b8b_b.jpg
                    [partyplan_min_product_price] => 150.00
                    [partyplan_max_product_price] => 500.00
                    [partyplan_product_qty] => 25
                )
        )
)

需要根据元素的增量得到

[partyplan_select_products]
的个数。

php arrays multidimensional-array sum grouping
2个回答
0
投票

试试这个,

function array_column_recursive(array $haystack, $needle) {
    $found = [];
    array_walk_recursive($haystack, function($value, $key) use (&$found, $needle) {
        if ($key == $needle)
            $found[] = $value;
    });
    return $found;
}

这是递归数组列函数。它不会考虑数组中所示的任何随机键。 然后,

你会得到,

$sum_arr = array_column_recursive($your_arr, 'partyplan_select_products');

最后得到总数, 您可以使用 array_sum 函数。

$count = array_sum($sum_arr);

希望这对您有帮助。


0
投票

您可以使用

foreach
循环并保存传递字段的总和。

$total_products = 0;

$field = 'partyplan_select_products';

foreach ($friends as $friend) {
    $total_products += array_sum(array_column($friend, $field));
}

print_r("$field: " . $total_products);

这是演示链接:https://3v4l.org/jdbv0

© www.soinside.com 2019 - 2024. All rights reserved.