'Count the Multi Deimensional array elments
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
)
)
)
Need to get the count of the [partyplan_select_products] according to the increment in the elements.
Solution 1:[1]
Try this,
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;
}
This is recursive array column function. It won't consider any keys which are random as shown in your array. Then,
You will get,
$sum_arr = array_column_recursive($your_arr, 'partyplan_select_products');
And finally to get total count, you can use array_sum function.
$count = array_sum($sum_arr);
I hope this will help you.
Solution 2:[2]
You can use foreach loop and save a sum of the passed field.
$total_products = 0;
$field = 'partyplan_select_products';
foreach ($friends as $friend) {
$total_products += array_sum(array_column($friend, $field));
}
print_r("$field: " . $total_products);
Here's the demo link: https://3v4l.org/jdbv0
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Rahul |
| Solution 2 | Hitesh |
