對於PHP專家來說,這應該很容易。我無法通過update_user_meta函數在Wordpress中存儲和拉取數組。在Wordpress用戶元數據庫中存儲PHP數組
所以我有一個關聯數組建立像這樣:
Array ( [film_genres] => Array ( [action] => 50 [comedy] => 50 [crime] => 50 [documentary] => 50 [drama] => 50 [family] => 50 [horror] => 50 [romantic] => 50 [sci-fi] => 50 [thriller] => 50 ) [film_types] => Array ( [blockbuster] => 0 [independent] => 0 ) [film_eras] => Array ( [1920s_1940s] => 0 [1950s_1960s] => 0 [1970s_1980s] => 0 [1990s_2000s] => 0 [post_2010] => 0 [pre_1920s] => 0 ) [last_updated] => 2011-10-12 21:21:28 )
但是,當我去更新通過這個數據在用戶的元數據表:
update_user_meta($ USER_ID,$ meta_key,$ meta_value,$ prev_value)
將數據正確放入數據庫,但是當我調用數據並將新數組打印到屏幕上時,它在數組內具有[0]的嵌套數組鍵,就像這樣:
Array ( [0] => Array ( [film_genres] => Array ( [action] => 50 [comedy] => 50 [crime] => 50 [documentary] => 50 [drama] => 50 [family] => 50 [horror] => 50 [romantic] => 50 [sci-fi] => 50 [thriller] => 50 ) [film_types] => Array ( [blockbuster] => 0 [independent] => 0 ) [film_eras] => Array ( [1920s_1940s] => 0 [1950s_1960s] => 0 [1970s_1980s] => 0 [1990s_2000s] => 0 [post_2010] => 0 [pre_1920s] => 0 ) [last_updated] => 2011-10-12 21:21:28 ) )
我怎樣才能得到它存儲數組完全像我的第一個數組?我通過WP命令拉動元數值數組:
$wp_user_film_prefs_arr = get_user_meta($wp_user_id, $wp_user_film_prefs_key_label, false);
有什麼我做錯了嗎?提前致謝!!!
是的,你是對的!我最初選擇了錯誤的想法,認爲它會返回數組,就像我的數組數組一樣,但它正在重新調用元數據字段的數組。更改爲true返回值,然後將其作爲數組存儲爲「maybe_unserialize($ wp_user_film_prefs_val);」謝啦! – chuuke
太棒了!謝謝! – Magico
想想一個數組作爲一個單值是很難讓你頭腦發熱的 – henrywright