这对PHP专家来说应该很容易。我无法通过update_user_meta函数在Wordpress中存储和提取数组。
所以我有一个像这样构建的关联数组:
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 );
我有什么问题吗?在此先感谢!!!
答案 0 :(得分:19)
您需要将最后一个参数从false
设置为true
:
$wp_user_film_prefs_arr = get_user_meta( $wp_user_id, $wp_user_film_prefs_key_label, true );
第三个参数是$single
:
(boolean)(可选)如果是元数据字段的true返回值,如果为false则返回一个数组。
默认值:false
这可能听起来在你的眼中反效果,但元数据字段可以包含多个值。在您的情况下,您不需要它,但单个值。单个值就是你的数组。
答案 1 :(得分:0)
您是否将serialize()
视为str并将此str存储到数据库中,并在从db获取后将unserialize()
str存入数组?