可能有更好的方式来陈述这个问题,但我正在努力解决这个问题。我所知道的。
在Woocommerce上,我试图获得每颗星(1-5)的_wc_rating_count,还有两种产品;然后我想结合主题;例如,我将两个产品的评论合并在一个页面上。
我这样做:
$product_id = array( 2146, 2166 );
$ratings = array(5, 4, 3, 2, 1);
foreach ($product_id as $pid) :
$product = wc_get_product($pid);
foreach ($ratings as $rating) :
$rating_count = $product->get_rating_count($rating);
$percentage = ($rating_count / $count) * 100 . "%";
//echo $rating_count . '<br/>';
endforeach;
endforeach;
问题是,我得到了10个项目的数组(2个产品x 5个星级(5,4,3,2,1),我需要合并这些值。
11
1
0
0
2
8
0
0
0
1
我需要的地方
19
1
0
0
3
你知道我如何从嵌入式foreach中获取两个数组并根据$ ratings数组合并它们吗?
感谢您的帮助:)
答案 0 :(得分:1)
所以这就是你想要实现的目标:
$product_ids = array( 2146, 2166 );
$ratings = array(5, 4, 3, 2, 1);
$rate_countings = array();
foreach ($product_ids as $pid){
$product = wc_get_product($pid);
foreach ($ratings as $rating){
$rating_count = $product->get_rating_count($rating);
//$percentage = ($rating_count / $count) * 100 . "%";
if( !isset($rate_countings[$rating]) ){
$rate_countings[$rating] = 0;
}
$rate_countings[$rating] += $rating_count;
}
}
var_dump($rate_countings);