Php 在产品页面上显示阵列中的产品属性值
我使用以下代码在产品页面上检索和显示我的产品属性值:Php 在产品页面上显示阵列中的产品属性值,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我使用以下代码在产品页面上检索和显示我的产品属性值: $discounts = get_post_meta( $product->id, '_my_discounts' ); \u我的折扣是一个包含不同折扣的数组。每个折扣包含不同的信息(最小数量,折扣,类型,是扁平的等)。当我使用echo var_dump($折扣)时 我得到以下输出(我有两个折扣): 我的问题是,如何在[“折扣”]中检索数组中的最高折扣并将其显示在产品页面上?例如,上述产品有两个折扣$100和$293。我想检索两者中最
$discounts = get_post_meta( $product->id, '_my_discounts' );
\u我的折扣
是一个包含不同折扣的数组。每个折扣包含不同的信息(最小数量
,折扣
,类型
,是扁平的
等)。当我使用echo var_dump($折扣)时代码>
我得到以下输出(我有两个折扣):
我的问题是,如何在[“折扣”]
中检索数组中的最高折扣并将其显示在产品页面上?例如,上述产品有两个折扣$100和$293。我想检索两者中最高的一个,并将其显示在我的产品页面上
我尝试了以下方法,但无效:
foreach ( $discounts as $highest_discount ) :
$discount_value = wc_get_product_terms( $product->id, $highest_discount['discount'], array( 'fields' => 'names' ) );
echo $discount_value;
endforeach;
任何帮助都将不胜感激。您只需编写一个小函数,查找折扣最高的数组元素
function find_highest_discount($discounts){
$max_discount=0;
foreach($discounts as $ds0){
if ($ds0['discount']>$max_discount) $max_discount=$ds0['discount'];
}
return $max_discount;
}
用法
更新:可以创建类似的函数以获得最低的数字
function find_lowest_discount($discounts){
$min_discount=array_values($discounts)[0]['discount'];
foreach($discounts as $ds0){
if ($ds0['discount']<$min_discount) $min_discount=$ds0['discount'];
}
return $min_discount;
}
函数查找最低折扣($折扣){
$min_折扣=数组_值($折扣)[0]['折扣];
foreach(折扣为$ds0){
如果($ds0['discount'],您需要比较循环中的折扣,以便能够看到witch是最高的。我只看到您在atm上重复了它们。请确保您在此处传递了正确的参数$highest\u discount=find\u highest\u discount($discounts[0])。-[0]很重要。如果仍然不走运,请将最新的代码状态添加到您的问题中,作为更新我刚刚在本地主机上测试了我的代码,它给出了正确的输出(293)。我已更新了您上次请求的答案。请检查。
$discounts = get_post_meta( $product->id, '_my_discounts' );
$highest_discount=find_highest_discount($discounts[0]);
function find_lowest_discount($discounts){
$min_discount=array_values($discounts)[0]['discount'];
foreach($discounts as $ds0){
if ($ds0['discount']<$min_discount) $min_discount=$ds0['discount'];
}
return $min_discount;
}