在 WordPress 用户元数据库中存储 PHP 数组
对于 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 );
我做错了什么吗?提前致谢!!!
This should be easy for a PHP expert. I am having trouble storing and pulling arrays in Wordpress through the update_user_meta function.
So I have an associative array built like so:
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 )
But when I go to update this data in the user meta table via:
update_user_meta( $user_id, $meta_key, $meta_value, $prev_value )
The data gets put in the db properly, but when i call the data back and print the new array to screen, it has a nested array key of [0] within the array, like this:
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 ) )
How can I get it to store the array exactly like my first array? I am pulling the meta value array via the WP command:
$wp_user_film_prefs_arr = get_user_meta( $wp_user_id, $wp_user_film_prefs_key_label, false );
Is there something I'm doing wrong? Thanks in advance!!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将最后一个参数从
false
设置为true
:第三个参数是
$single
:这在您看来可能听起来适得其反,但元数据字段可以包含多个值。在您的情况下,您不需要那个,而是单个值。单个值是您的数组。
另请参阅:
获取用户元
CodexYou need to set the last parameter from
false
totrue
:That third parameter is
$single
:That might sound contra-productive in your eyes, but the meta data field can contain multiple values. In your case you don't need that, but the single value. The single value is your array.
See as well:
get user meta
Codex您是否将
serialize()
视为 str 并将该 str 存储到数据库中,并在从 db 获取后unserialize()
将 str 存储到数组中?Have you consider
serialize()
it as a str and store this str into your db, andunserialize()
the str into array after getting from db?