计算多维数组元素

时间:2017-01-11 06:55:02

标签: arrays

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]的计数。

1 个答案:

答案 0 :(得分: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);

我希望这会对你有所帮助。