繁体   English   中英

打印多维数组值

[英]Printing multidimensional array values

我有一个这种类型的数组,使用这个代码

$myarray = get_post_meta( $product->get_ID(), 'wcb2b_product_group_prices' );
print_r($myarray);

打印我:

Array ( [0] => Array ( [428] => Array ( [regular_price] => [sale_price] => ) [449] => Array ( [regular_price] => 20.00 [sale_price] => ) [9609] => Array ( [regular_price] => 20.00 [sale_price] => ) ) ) 

更新代码:

$myarray = get_post_meta( $product->get_ID(), 'wcb2b_product_group_prices' );

//print_r($myarray);
 
foreach ($myarray as $key => $value) {
    // print_r($value);
    foreach ($value as $key2 => $value2) {
        // print_r($value2);
        foreach ($value2 as $key3 => $value3) {
            echo $value3;
        }
    }
}

我将能够打印 [regular_price][sale_price] 值。

你是怎样做的?

感谢帮助

下面应该做你正在寻找的......

if ( $group_prices = get_post_meta( $product->get_ID(), 'wcb2b_product_group_prices', true ) ) {
    foreach ( $group_prices as $group => $group_price ) {
        $regular_price = $group_price[ 'regular_price' ] ?? null;
        $sale_price = $group_price[ 'sale_price' ] ?? null;

        if ( $regular_price ) {
            echo '<strong>Regular price for group ' . $group . ':</strong> ' . wc_price( $regular_price ) . '<br>';
        }
        
        if ( $sale_price ) {
            echo '<strong>Sale price for group ' . $group . ':</strong> ' . wc_price( $sale_price ) . '<br>';
        }
    }
}

这将 output 类似于...

<strong>Regular price for group 449:</strong> $20.00<br>
<strong>Regular price for group 9609:</strong> $20.00<br>
  1. 首先,我们将get_post_meta的第三个参数设置为true ,这将删除不必要的数组的第一级。
  2. 然后我们遍历这些组并获得每个组的常规价格和销售价格,在打印之前检查它们是否具有价值。
  3. 最后,我们使用 WooCommerce 核心wc_price() function 打印价格,它将使用货币符号格式化价格值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM