Php 在Woocommerce中将两个产品星级与foreach合并
也许有更好的方式来陈述这个问题,但我正在用我所知道的来工作 在Woocommerce上,我试图获得每颗星(1-5)的“wc评分”,但也包括两种产品;然后我想结合主题;e、 例如,我将两个产品的评论合并到一个页面,一个图表中 我是这样做的:Php 在Woocommerce中将两个产品星级与foreach合并,php,wordpress,foreach,woocommerce,array-merge,Php,Wordpress,Foreach,Woocommerce,Array Merge,也许有更好的方式来陈述这个问题,但我正在用我所知道的来工作 在Woocommerce上,我试图获得每颗星(1-5)的“wc评分”,但也包括两种产品;然后我想结合主题;e、 例如,我将两个产品的评论合并到一个页面,一个图表中 我是这样做的: $product_id = array( 2146, 2166 ); $ratings = array(5, 4, 3, 2, 1); foreach ($product_id as $pid) : $product = wc_get_produc
$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;
我需要它在哪里
19
1
0
0
3
您知道如何从嵌入式foreach中获取两个数组,并基于$ratings数组合并它们吗
感谢您的帮助:)以下是您希望实现的目标:
$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);
$count
变量中有什么?你所说的合并的值是什么意思?嗨,皮特。抱歉,$count是评论的总数(23),我需要按星级分类。合并值意味着11和8需要合并(两个产品的5颗星),然后是1和0,0和0,0和0,以及1和3。所以您需要每个产品的所有$rating\u count
的总和?或者你想得到什么?或者,是否要对所有产品的所有$rating\u count
进行求和,但按评级类型(5,4,3,2,1)分组?该$count已经是两种产品的总和。我需要将产品的每个$rating(星级)合并在一起。在上面的例子中,我能够得到每种产品每颗星的评级计数,但这让我将两个阵列合并为一个阵列。我得把1和6相加,把2和7相加,等等,但是感觉不对。哦,我明白了。你只是想得到每个产品的平均评级?像2146
有3.16
评级和2166
a4.37
评级?(示例数字)您是否愿意按照总体评级对其进行排序?
$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);