Php 如何比较循环中的值?
我收到命令了。每个订单都有一个类型,即“香草”和一个尺寸,即“迷你”和数量 如果有5个迷你瓦尼拉,我希望它显示一次,而不是5次。我还要清点数量 这是我尝试过的许多事情之一Php 如何比较循环中的值?,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我收到命令了。每个订单都有一个类型,即“香草”和一个尺寸,即“迷你”和数量 如果有5个迷你瓦尼拉,我希望它显示一次,而不是5次。我还要清点数量 这是我尝试过的许多事情之一 $prevSize = null; $prevType= null; foreach ($orders as $order){ echo '<tr>'; $new_order = new WC_Order(); $order_items = $new_order->get_items(); f
$prevSize = null;
$prevType= null;
foreach ($orders as $order){
echo '<tr>';
$new_order = new WC_Order();
$order_items = $new_order->get_items();
foreach ($order_items as $order_item ){
$currentSize = $order_item['pa_size'];
$currentType = wp_get_post_terms( $order_item['product_id'],'pa_ct');
$currentType = $currentType[0]->name;
if($prevSize != $currentSize){
echo $currentType . '<br>';
echo $currentSize . '<br>';
}
$count += $order_item['qty'];
echo $count;
}
$prevSize = $currentSize;
$prevType = $currentType;}
我们已经分析了您的查询,根据您的需要,以下是我的解决方案:
$order_type = array();
foreach ($orders as $order){
echo '<tr>';
$new_order = new WC_Order();
$order_items = $new_order->get_items();
$count = 0;
foreach ($order_items as $order_item ){
$currentSize = $order_item['pa_size'];
$currentType = wp_get_post_terms( $order_item['product_id'],'pa_ct');
$currentType = $currentType[0]->name;
$temp_order_type = $currentSize.'_'.$currentType;
if(!in_array($temp_order_type, $order_type)){
$order_type[] = $temp_order_type;
echo $currentType . '<br>';
echo $currentSize . '<br>';
}
$count += $order_item['qty'];
}
echo $count;
}
上述代码将显示每种订单类型一次,而不是多次显示。并显示每个订单中的订购数量 在$prevType之后插入$count并将其赋值为0。要连接,请使用点而不是加号$count=0 |$count.=$order_item['qty']为什么不使用一个关联数组,该数组带有一个表示产品最小值的唯一键和表示数量的值?在每次迭代中,将数量添加到数组中,并在退出循环后显示数组。