我為每個帖子都有一組欄位。并非每個帖子的所有欄位都有值。我嘗試獲取包含所有欄位的陣列并獲取空值。如何只獲得非空值?
我的代碼:
$parthers_shops_prices = array(
get_field( 'price_1', $product_id ),
get_field( 'price_2', $product_id ),
get_field( 'price_3', $product_id ),
get_field( 'price_4', $product_id ),
get_field( 'price_5', $product_id ),
get_field( 'price_6', $product_id ),
get_field( 'price_7', $product_id ),
get_field( 'price_8', $product_id ),
);
我得到:陣列( [0] => 199 [1] => [2] => [3] => 299 [4] => [5] => [6] => [7] => )。
我想得到:陣列( [0] => 199 [1] => 299 )。
uj5u.com熱心網友回復:
加載您的值后,此附加行應為您提供所需的結果:
$parthers_shops_prices = array_filter($parthers_shops_prices);
這應該輸出
Array ( [0] => 199 [3] => 299 )
如果您需要另外重新索引您的陣列,則可以執行
$parthers_shops_prices = array_values($parthers_shops_prices);
這應該改為輸出
Array ( [0] => 199 [1] => 299 )
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/471377.html
上一篇:wordpress中媒體庫的問題
