我试图获得客户购买产品的时间,但未能获得总数量或得到结果。如何通过数组中的product_id缩短。请帮忙我如何缩短它们。 Here is full code 我之前已经应用了anser,但仍然没有工作,输出保持与之前相同
$lifetime_total = array();
foreach($items as $item) {
$_product_id = $item['product_id'];
$_product_qty = $item['qty'];
if(isset ($_product_id)){
if(array_key_exists($_product_id, $lifetime_total))
{
$lifetime_total[$_product_id] += $_product_qty;
}
else{
$lifetime_total[$_product_id] = $_product_qty;
}
}
}
foreach( $lifetime_total as $key => $qty){
echo '('. get_the_title($key). ')# You bought '.$qty.' Times<br>';
}
当前输出为
(Product 2)# You bought 1 Times
(Product 1)# You bought 1 Times
(Product 1)# You bought 1 Times
(Product 2)# You bought 1 Times
(Product 1)# You bought 1 Times
(Product 2)# You bought 1 Times
(Product 1)# You bought 1 Times
(Product 2)# You bought 1 Times
但我需要的结果应该是
(Product 2)# You bought 4 Times
(Product 1)# You bought 4 Times
这里是var_dump
array(1) { [20]=> array(13) { ["name"]=> string(13) "Test Products" ["type"]=> string(9) "line_item" ["item_meta"]=> array(9) { ["_qty"]=> array(1) { [0]=> string(1) "1" } ["_tax_class"]=> array(1) { [0]=> string(0) "" } ["_product_id"]=> array(1) { [0]=> string(1) "4" } ["_variation_id"]=> array(1) { [0]=> string(1) "0" } ["_line_subtotal"]=> array(1) { [0]=> string(3) "500" } ["_line_total"]=> array(1) { [0]=> string(3) "500" } ["_line_subtotal_tax"]=> array(1) { [0]=> string(1) "0" } ["_line_tax"]=> array(1) { [0]=> string(1) "0" } ["_line_tax_data"]=> array(1) { [0]=> string(45) "a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}" } } ["item_meta_array"]=> array(9) { [177]=> object(stdClass)#458 (2) { ["key"]=> string(4) "_qty" ["value"]=> string(1) "1" } [178]=> object(stdClass)#457 (2) { ["key"]=> string(10) "_tax_class" ["value"]=> string(0) "" } [179]=> object(stdClass)#456 (2) { ["key"]=> string(11) "_product_id" ["value"]=> string(1) "4" } [180]=> object(stdClass)#455 (2) { ["key"]=> string(13) "_variation_id" ["value"]=> string(1) "0" } [181]=> object(stdClass)#454 (2) { ["key"]=> string(14) "_line_subtotal" ["value"]=> string(3) "500" } [182]=> object(stdClass)#453 (2) { ["key"]=> string(11) "_line_total" ["value"]=> string(3) "500" } [183]=> object(stdClass)#452 (2) { ["key"]=> string(18) "_line_subtotal_tax" ["value"]=> string(1) "0" } [184]=> object(stdClass)#451 (2) { ["key"]=> string(9) "_line_tax" ["value"]=> string(1) "0" } [185]=> object(stdClass)#450 (2) { ["key"]=> string(14) "_line_tax_data" ["value"]=> string(45) "a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}" } } ["qty"]=> string(1) "1" ["tax_class"]=> string(0) "" ["product_id"]=> string(1) "4" ["variation_id"]=> string(1) "0" ["line_subtotal"]=> string(3) "500" ["line_total"]=> string(3) "500" ["line_subtotal_tax"]=> string(1) "0" ["line_tax"]=> string(1) "0" ["line_tax_data"]=> string(45) "a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}" } } array(2) { [11]=> array(13) { ["name"]=> string(18) "Warranty Product 1" ["type"]=> string(9) "line_item" ["item_meta"]=> array(9) { ["_qty"]=> array(1) { [0]=> string(1) "1" } ["_tax_class"]=> array(1) { [0]=> string(0) "" } ["_product_id"]=> array(1) { [0]=> string(2) "63" } ["_variation_id"]=> array(1) { [0]=> string(1) "0" } ["_line_subtotal"]=> array(1) { [0]=> string(1) "0" } ["_line_total"]=> array(1) { [0]=> string(1) "0" } ["_line_subtotal_tax"]=> array(1) { [0]=> string(1) "0" } ["_line_tax"]=> array(1) { [0]=> string(1) "0" } ["_line_tax_data"]=> array(1) { [0]=> string(45) "a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}" } } ["item_meta_array"]=> array(9) { [96]=> object(stdClass)#448 (2) { ["key"]=> string(4) "_qty" ["value"]=> string(1) "1" } [97]=> object(stdClass)#447 (2) { ["key"]=> string(10) "_tax_class" ["value"]=> string(0) "" } [98]=> object(stdClass)#446 (2) { ["key"]=> string(11) "_product_id" ["value"]=> string(2) "63" } [99]=> object(stdClass)#445 (2) { ["key"]=> string(13) "_variation_id" ["value"]=> string(1) "0" } [100]=> object(stdClass)#444 (2) { ["key"]=> string(14) "_line_subtotal" ["value"]=> string(1) "0" } [101]=> object(stdClass)#443 (2) { ["key"]=> string(11) "_line_total" ["value"]=> string(1) "0" } [102]=> object(stdClass)#442 (2) { ["key"]=> string(18) "_line_subtotal_tax" ["value"]=> string(1) "0" } [103]=> object(stdClass)#441 (2) { ["key"]=> string(9) "_line_tax" ["value"]=> string(1) "0" } [104]=> object(stdClass)#440 (2) { ["key"]=> string(14) "_line_tax_data" ["value"]=> string(45) "a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}" } } ["qty"]=> string(1) "1" ["tax_class"]=> string(0) "" ["product_id"]=> string(2) "63" ["variation_id"]=> string(1) "0" ["line_subtotal"]=> string(1) "0" ["line_total"]=> string(1) "0" ["line_subtotal_tax"]=> string(1) "0" ["line_tax"]=> string(1) "0" ["line_tax_data"]=> string(45) "a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}" } [12]=> array(13) { ["name"]=> string(18) "Warranty Product 2" ["type"]=> string(9) "line_item" ["item_meta"]=> array(9) { ["_qty"]=> array(1) { [0]=> string(1) "1" } ["_tax_class"]=> array(1) { [0]=> string(0) "" } ["_product_id"]=> array(1) { [0]=> string(2) "18" } ["_variation_id"]=> array(1) { [0]=> string(1) "0" } ["_line_subtotal"]=> array(1) { [0]=> string(1) "0" } ["_line_total"]=> array(1) { [0]=> string(1) "0" } ["_line_subtotal_tax"]=> array(1) { [0]=> string(1) "0" } ["_line_tax"]=> array(1) { [0]=> string(1) "0" } ["_line_tax_data"]=> array(1) { [0]=> string(45) "a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}" } } ["item_meta_array"]=> array(9) { [105]=> object(stdClass)#439 (2) { ["key"]=> string(4) "_qty" ["value"]=> string(1) "1" } [106]=> object(stdClass)#438 (2) { ["key"]=> string(10) "_tax_class" ["value"]=> string(0) "" } [107]=> object(stdClass)#437 (2) { ["key"]=> string(11) "_product_id" ["value"]=> string(2) "18" } [108]=> object(stdClass)#436 (2) { ["key"]=> string(13) "_variation_id" ["value"]=> string(1) "0" } [109]=> object(stdClass)#435 (2) { ["key"]=> string(14) "_line_subtotal" ["value"]=> string(1) "0" } [110]=> object(stdClass)#434 (2) { ["key"]=> string(11) "_line_total" ["value"]=> string(1) "0" } [111]=> object(stdClass)#433 (2) { ["key"]=> string(18) "_line_subtotal_tax" ["value"]=> string(1) "0" } [112]=> object(stdClass)#432 (2) { ["key"]=> string(9) "_line_tax" ["value"]=> string(1) "0" } [113]=> object(stdClass)#431 (2) { ["key"]=> string(14) "_line_tax_data" ["value"]=> string(45) "a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}" } } ["qty"]=> string(1) "1" ["tax_class"]=> string(0) "" ["product_id"]=> string(2) "18" ["variation_id"]=> string(1) "0" ["line_subtotal"]=> string(1) "0" ["line_total"]=> string(1) "0" ["line_subtotal_tax"]=> string(1) "0" ["line_tax"]=> string(1) "0" ["line_tax_data"]=> string(45) "a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}" } }
答案 0 :(得分:0)
$_product_qty
似乎是一个字符串数组
尝试将代码更改为:
$lifetime_total = array();
foreach($items as $item) {
$_product_id = $item['product_id'];
$_product_qty_array = $item['qty'];
$_product_qty = 0;
if(count($_product_qty_array) > 0){
$_product_qty = intval($_product_qty_array[0]);
}
if(isset ($_product_id)){
if(array_key_exists($_product_id, $lifetime_total))
{
$lifetime_total[$_product_id] += $_product_qty;
}
else{
$lifetime_total[$_product_id] = $_product_qty;
}
}
}
foreach( $lifetime_total as $key => $qty){
echo '('. get_the_title($key). ')# You bought '.$qty.' Times<br>';
}
答案 1 :(得分:0)
这种方法可能有所帮助:
$data = array( ... ); // your beginning array
$result = array(); // the end array
foreach ( $data as $key => $value ) {
if ( !isset( $result[ $key ] ) ) {
$result[ $key ] = $value;
} else {
$result[ $key ] += $value;
}
}