使用UPDATE_USER_META存储数组时出现问题

时间:2011-03-29 作者:Jarred

我正在编写一个函数,将ID号添加到数组中,并将数组放入usermeta中。$_GET[\'auction\']post_id.

功能如下:

$reminders = get_user_meta( $current_user->ID, "reminders" );
print_r( $reminders );
if( in_array( $_GET[\'auction\'], $reminders ) ) {
    echo "Failed: Auction already in list";
} else {
    array_push( $reminders, intval( $_GET[\'auction\'] ) );
    if ( update_user_meta( $current_user->ID, "reminders", $reminders ) ) {
        echo "Success";
    } else {
        echo "Failed: Could not update user meta";
    }
}
print_r( $reminders );
以下是添加一次拍卖后的输出:

Array ( ) 
Success
Array ( [0] => 7 ) 
以下是添加两次拍卖后的输出:

Array ( [0] => Array ( [0] => 7 ) ) 
Success
Array ( [0] => Array ( [0] => 7 ) [1] => 73 )
以下是添加三次拍卖后的结果:

Array ( [0] => Array ( [0] => Array ( [0] => 7 ) [1] => 73 ) ) 
Success
Array ( [0] => Array ( [0] => Array ( [0] => 7 ) [1] => 73 ) [1] => 0 )
请注意,使用将新元素添加到数组中array_push 工作正常。但是,当数组存储在usermeta中,然后再次检索时,数组中的最后一个元素被放入自己的数组中,从而创建了一个无限维数组。我更喜欢保持这个数组是一维的。

有什么方法可以让我跑吗update_user_metaget_user_meta 在不改变阵列结构的情况下?

5 个回复
最合适的回答,由SO网友:kaiser 整理而成

很长时间没有使用该函数了,但我想您的问题是将一个数组推入一个数组。所以检查一下intval($_GET[\'auction\']) 是一个数组:

echo \'<pre>\';
print_r(intval($_GET[\'auction\']));
echo \'</pre>\';
Edit #1: 您可能需要从该数组中获取值,然后array\\u推送它。也许是这样的array_push( $reminders, $_GET[\'auction\'][0]) ); - 如果只添加一个值。你也可以这样做$reminders[] = $_GET[\'auction\'][0]; 将其添加到数组末尾。

Edit #2: 从核心文件来看:是的。update_user_meta() 只是的别名update_metadata() 它接受ID+值,并将其作为和数组放入数据库。

// From /wp-includes/meta.php ~ line 135
$where = array( $column => $object_id, \'meta_key\' => $meta_key );

if ( !empty( $prev_value ) ) {
    $prev_value = maybe_serialize($prev_value);
    $where[\'meta_value\'] = $prev_value;
}

SO网友:Shaun

我也有同样的问题。在“get\\u user\\u meta”中添加“true”对我很有用。例如:

FROM:

$reminders = get_user_meta($current_user->ID,"reminders");

TO:

$reminders = get_user_meta($current_user->ID,"reminders",true);

SO网友:FueledPublishing

出现了相同的问题,并通过以下一点解决了此问题,即将所有新值放入保存为用户元数据的单个数组中:

//Where $access_key is the next (added) value

$get_access_keys_from_wp = get_user_meta($user_id,\'wsm_capability\');
$current_access_keys = $get_access_keys_from_wp[0];
$new_access_keys = array();
$new_access_keys[]=$access_key;

foreach($current_access_keys as $key => $value){
    $new_access_keys[]=$value;
}
delete_user_meta( $user_id, \'wsm_capability\');//Clear out the meta data...
update_user_meta( $user_id, \'wsm_capability\', $new_access_keys);
保存/更新元密钥之前的数组(来自get\\u user\\u meta):

Array
(
    [0] => access_9
)
结果数组(元更新后)添加“access\\u 5”的值:

Array
(
    [0] => access_5
    [1] => access_9
)
如果需要将新值添加到数组末尾,请执行以下操作:

//Where $access_key is the next (added) value    
$get_access_keys_from_wp = get_user_meta($user_id,\'wsm_capability\');
$current_access_keys = $get_access_keys_from_wp[0];
$new_access_keys = array();

foreach($current_access_keys as $key => $value){
    $new_access_keys[]=$value;
}
$new_access_keys[]=$access_key;
然后更新元。。。

布莱恩

SO网友:Jarred

问题似乎在于序列化/取消序列化数组,所以我只是将函数重写为逗号字符串:

        $reminders = get_user_meta($current_user->ID,"reminders",TRUE);
        if(is_int(strpos($reminders,$_GET[\'auction\']))) {
            echo "Failed: Auction already in list";
        } else {
            $reminders .= ",".intval($_GET[\'auction\']);
            if(substr($reminders,0,1) == ",") { //Remove leading comma if it exists
                $reminders = substr($reminders,1,strlen($reminders)-1);
            }
            if(update_user_meta($current_user->ID,"reminders",$reminders)) {
                echo "Success";
            } else {
                echo "Failed: Could not update user meta";
            }
        }

SO网友:Yaron

肖恩的回答是正确的,但我觉得需要更多的澄清。您可以将数组放入用户元条目中,但当您检索它时,仍然需要将单个参数设置为true来检索它,否则您将获得一个数组的数组。这是我的代码:

$past_orders = get_user_meta($order_userID, \'qr-replacement-orders\',true);

//see if this new order has already been processed
if(in_array($_GET["oid"],$past_orders))
{
     echo \'<p>This order has already been processed.</p>\';
    //debug
    //var_dump($past_orders);   
}
else
{
      //add the order number to the array of past orders and store it
     //if list is empty, initialize it to empty array
     if($past_orders == \'\')
     {
        $past_orders = array();
     }
     //add the new order to the list
    array_push($past_orders, $_GET["oid"]);

    //add the new list to the DB
    update_user_meta($order_userID, \'qr-replacement-orders\',$past_orders);

    echo \'<p>Your card has been purchased and will be sent to you in the mail.  Thanks!</p>\';                       

    //debug: confirm it worked
    //$past_orders = get_user_meta($order_userID, \'qr-replacement-orders\',true);
    //var_dump($past_orders);   
}

结束

相关推荐