質問

Arrayに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 );

1つのオークションを追加した後の出力は次のとおりです。

Array ( ) 
Success
Array ( [0] => 7 ) 

2つのオークションを追加した後の出力は次のとおりです。

Array ( [0] => Array ( [0] => 7 ) ) 
Success
Array ( [0] => Array ( [0] => 7 ) [1] => 73 )

そして、3つのオークションを追加した後の出力は次のとおりです。

Array ( [0] => Array ( [0] => Array ( [0] => 7 ) [1] => 73 ) ) 
Success
Array ( [0] => Array ( [0] => Array ( [0] => 7 ) [1] => 73 ) [1] => 0 )

使用して配列に新しい要素を追加することに注意してください array_push 正常に動作します。しかし、配列がに保存されている場合 Usermeta その後、再び取得すると、配列の最後の要素が独自の配列に配置され、無限に寸法配列が作成されました。この配列を1次元に保ちたいと思います。

私が走ることができる方法はありますか? update_user_metaget_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;
}

他のヒント

同じ問題がありました。 「get_user_meta」に「true」を追加することは私のために働きました。例えば:

から:

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

結果の配列(メタアップデート後)「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;

次に、メタを更新します...

ブライアン

問題はアレイのシリアル化/非正規化に関するものであるように思われたので、私はこの関数をカンマのスケートされた文字列に書き直しました。

        $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";
            }
        }

ショーンの答えは正しかったが、もっと明確にする必要があると感じている。ユーザーメタエントリに配列を入力することはできますが、それを取得すると、単一の引数を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);   
}
ライセンス: CC-BY-SA帰属
所属していません wordpress.stackexchange
scroll top