[英]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>
get_post_meta
的第三個參數設置為true
,這將刪除不必要的數組的第一級。wc_price()
function 打印價格,它將使用貨幣符號格式化價格值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.