在 WordPress 用户元数据库中存储 PHP 数组

发布于 2024-12-09 16:29:43 字数 2326 浏览 1 评论 0原文

对于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夢归不見 2024-12-16 16:29:43

您需要将最后一个参数从 false 设置为 true

$wp_user_film_prefs_arr = get_user_meta( $wp_user_id, $wp_user_film_prefs_key_label, true );

第三个​​参数是 $single

(布尔值)(可选)如果为 true,则返回元数据字段的值,如果为 false,则返回一个数组。

默认值:假

这在您看来可能听起来适得其反,但元数据字段可以包含多个值。在您的情况下,您不需要那个,而是单个值。单个值是您的数组。

另请参阅:获取用户元Codex

You need to set the last parameter from false to true:

$wp_user_film_prefs_arr = get_user_meta( $wp_user_id, $wp_user_film_prefs_key_label, true );

That third parameter is $single:

(boolean) (optional) If true return value of meta data field, if false return an array.

Default: false

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 metaCodex

云归处 2024-12-16 16:29:43

您是否将 serialize() 视为 str 并将该 str 存储到数据库中,并在从 db 获取后 unserialize() 将 str 存储到数组中?

Have you consider serialize() it as a str and store this str into your db, and unserialize() the str into array after getting from db?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文