使用update_user_meta的问题存储数组
-
16-10-2019 - |
题
我正在编写一个函数,该函数将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_meta
和 get_user_meta
没有它改变我的数组的结构?
解决方案
已经有一段时间没有使用该功能,但是我想您的问题是您将数组推入数组。因此,检查是否 intval($_GET['auction'])
是一个数组:
echo '<pre>';
print_r(intval($_GET['auction']));
echo '</pre>';
编辑#1: 您可能需要从该数组中获取值,然后将其array_push获取。所以也许像 array_push( $reminders, $_GET['auction'][0]) );
- 如果您只添加一个值。你也可以做类似的事情 $reminders[] = $_GET['auction'][0];
将其添加到阵列的末端。
编辑#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;
}
其他提示
我有同样的问题。将“ true”添加到“ get_user_meta”中为我工作。例如:
从:
$reminders = get_user_meta($current_user->ID,"reminders");
至:
$reminders = get_user_meta($current_user->ID,"reminders",true);
存在相同的问题,并用这一点解决了它,这将所有新值放在一个单个数组中,保存为用户元数据:
//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_user_meta):
Array
(
[0] => access_9
)
结果数组(Meta更新之后)添加了“ Access_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;
然后更新元...
布莱恩
看来问题是序列化/不重新化数组,因此我只是将函数重写为逗号sperated字符串:
$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";
}
}
肖恩的回答是正确的,但我觉得需要更加澄清。您可以将数组放入用户Meta条目中,但是当您检索它时,您仍然需要使用单个参数设置为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);
}