2014-04-01 55 views
0

我在編寫wordpress中的語句時遇到了一些問題。 我的插件將自定義註冊字段存儲在數據庫中的一行中,可以說「custom_fields」。當我打印使用get_user_meta custom_fields我得到的所有儲存的信息,例如一個數組:檢查數組中是否存在任何東西

Array ([0] => Array ([invoice_company_name] => x [invoice_nip] => x [invoice_street] => x [invoice_street_number] => x [invoice_post_code] => x [invoice_city] => x [invoice_country] => x [birth_date] => x [birth_city] => x [district] => x)) 

我想,如果所有字段開頭的發票存在檢查。當然,'x'在哪裏有真正的價值。

好吧,我發現函數in_array(),所以嘗試做這樣的事情,但它不工作

$user_ID = get_current_user_id(); 
$all_meta_for_user = get_user_meta($user_ID, 'wp_s2member_custom_fields', false); 
print_r($all_meta_for_user); 
if (in_array("[invoice_company_name]", $all_meta_for_user)) { 
echo "exist";} else {echo 'dont exist';} 

而且我得到了「不存在」 :)什麼錯? 另外,我可以一次檢查所有的值嗎?有點像in_array([1st] & & [2nd] & & [3rd])?

謝謝!

+1

刪除括號:'in_array( 「[invoice_company_name]」,$ all_meta_for_user))'to'in_array(「invoice_comp any_name「,$ all_meta_for_user))' – Debflav

回答

1

如果陣列是多維的,你可以通過按鍵使用array_key_exists()搜索:

foreach($all_meta_for_user as $key=>$val){ 
    if (array_key_exists('invoice_company_name', $val)) 
    { 
     echo "exist in key ".$key; 
    } 
    else 
    { 
     echo "does not exist in key ".$key; 
    } 
} 

Demo

+0

非常感謝!它工作正如我想:) – zel

+0

@zel不客氣 –

2

嘗試像

if (in_array("invoice_company_name", $all_meta_for_user[0])) { 
相關問題