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

时间:2021-07-26 12:42:17

This should be easy for a PHP expert. I am having trouble storing and pulling arrays in Wordpress through the update_user_meta function.

这对PHP专家来说应该很容易。我无法通过update_user_meta函数在Wordpress中存储和拉取数组。

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 )

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:

数据被正确放入数据库,但是当我调用数据并将新数组打印到屏幕时,它在数组中有一个嵌套数组键[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
        )

)

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命令拉取元值数组:

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

$ 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!!!

有什么我做错了吗?提前致谢!!!

2 个解决方案

#1


18  

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

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

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

That third parameter is $single:

第三个参数是$ single:

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

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

Default: false

默认值: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

另见:获取用户metaCodex

#2


0  

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?

您是否将serialize()视为str并将此str存储到您的db中,并在从db获取之后将str反序列化()到数组中?

#1


18  

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

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

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

That third parameter is $single:

第三个参数是$ single:

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

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

Default: false

默认值: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

另见:获取用户metaCodex

#2


0  

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?

您是否将serialize()视为str并将此str存储到您的db中,并在从db获取之后将str反序列化()到数组中?