简体   繁体   English

打印多维数组值

[英]Printing multidimensional array values

I have an array of this type, Using this code我有一个这种类型的数组,使用这个代码

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

prints me:打印我:

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] => ) ) ) 

Updated code:更新代码:

$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;
        }
    }
}

I would be able to print the [regular_price][sale_price] Value.我将能够打印 [regular_price][sale_price] 值。

How do you do that?你是怎样做的?

Thanks for help感谢帮助

The below should do what you're looking for...下面应该做你正在寻找的......

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>';
        }
    }
}

This will output something like...这将 output 类似于...

<strong>Regular price for group 449:</strong> $20.00<br>
<strong>Regular price for group 9609:</strong> $20.00<br>
  1. First we set the 3rd parameter for get_post_meta to true which removes the first level of the array which is unnecessary.首先,我们将get_post_meta的第三个参数设置为true ,这将删除不必要的数组的第一级。
  2. We then loop through the groups and get regular and sale prices for each group, checking if they have values before printing.然后我们遍历这些组并获得每个组的常规价格和销售价格,在打印之前检查它们是否具有价值。
  3. Finally, we print the price using the WooCommerce core wc_price() function which will format the price value with the currency symbol.最后,我们使用 WooCommerce 核心wc_price() function 打印价格,它将使用货币符号格式化价格值。

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

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