I am writing a function that adds an ID number to an array and puts the array in the usermeta. $_GET['auction']
is the post_id
.
Below is the function:
$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 );
Here is the output after adding one auction:
Array ( )
Success
Array ( [0] => 7 )
Here is the output after adding two auctions:
Array ( [0] => Array ( [0] => 7 ) )
Success
Array ( [0] => Array ( [0] => 7 ) [1] => 73 )
And here is the output after adding three auctions:
Array ( [0] => Array ( [0] => Array ( [0] => 7 ) [1] => 73 ) )
Success
Array ( [0] => Array ( [0] => Array ( [0] => 7 ) [1] => 73 ) [1] => 0 )
Notice that adding a new element to the array using array_push
works fine. But when the array is stored in the usermeta then retrieved again, the last element in the array has been put into an array of its own, creating an infinitely dimensional array. I’d prefer to keep this array one dimensional.
Is there a way I can run update_user_meta
and get_user_meta
without it changing the structure of my array?