I'm storing a post_meta_key in WP. The value is an array.
I do a get_post_meta - which returns an empty array the first time. $single
is set to false. This array is called $voters
, as it stores a list of voters. The key is the user id, the value is an array, described next.
Then I loop through the inputs, sanitize them, and create another key=>value array called the current_vote
. This is the value set to the user id key.
I would like to insert the current_vote
as a value into the voters array. Right now, I'm trying this:
$voters[$voter_id] = $current_vote;
This properly creates a key from the $voter_id
, and put the current_vote
array as the value.
BUT! as the second vote comes in, it doesn't just insert another key into the existing array - it takes that first vote, and inserts it into a NEW array!
The first vote looks like this:
Array
(
[13] => Array
(
[13] => 0
[15] => 75
[21] => 0
[34] => 0
[16] => 0
[50] => 0
[28] => 0
[45] => 0
[10] => 0
[40] => 0
[41] => 0
[52] => 0
[22] => 0
[29] => 0
[23] => 0
[30] => 0
[48] => 0
[53] => 0
[38] => 0
[35] => 0
[61] => 0
[26] => 0
[9] => 0
[62] => 0
[54] => 0
[49] => 0
[14] => 0
[19] => 0
[42] => 0
[55] => 0
[5] => 0
[12] => 0
[46] => 0
[56] => 0
[32] => 0
[36] => 0
[2] => 0
[17] => 0
[4] => 0
[27] => 0
[44] => 0
[25] => 0
[57] => 0
[37] => 0
[3] => 0
[51] => 0
[31] => 0
[43] => 0
[47] => 0
[39] => 0
)
)
Once the second vote comes in, it looks like this:
Array
(
[0] => Array
(
[13] => Array
(
[13] => 0
[15] => 75
[21] => 0
[34] => 0
[16] => 0
[50] => 0
[28] => 0
[45] => 0
[10] => 0
[40] => 0
[41] => 0
[52] => 0
[22] => 0
[29] => 0
[23] => 0
[30] => 0
[48] => 0
[53] => 0
[38] => 0
[35] => 0
[61] => 0
[26] => 0
[9] => 0
[62] => 0
[54] => 0
[49] => 0
[14] => 0
[19] => 0
[42] => 0
[55] => 0
[5] => 0
[12] => 0
[46] => 0
[56] => 0
[32] => 0
[36] => 0
[2] => 0
[17] => 0
[4] => 0
[27] => 0
[44] => 0
[25] => 0
[57] => 0
[37] => 0
[3] => 0
[51] => 0
[31] => 0
[43] => 0
[47] => 0
[39] => 0
)
)
[4] => Array
(
[13] => 75
[15] => 0
[21] => 0
[34] => 0
[16] => 0
[50] => 0
[28] => 0
[45] => 0
[10] => 0
[40] => 0
[41] => 0
[52] => 0
[22] => 0
[29] => 0
[23] => 0
[30] => 0
[48] => 0
[53] => 0
[38] => 0
[35] => 0
[61] => 0
[26] => 0
[9] => 0
[62] => 0
[54] => 0
[49] => 0
[14] => 0
[19] => 0
[42] => 0
[55] => 0
[5] => 0
[12] => 0
[46] => 0
[56] => 0
[32] => 0
[36] => 0
[2] => 0
[17] => 0
[4] => 0
[27] => 0
[44] => 0
[25] => 0
[57] => 0
[37] => 0
[3] => 0
[51] => 0
[31] => 0
[43] => 0
[47] => 0
[39] => 0
)
)
so, the element is inserted correctly (new element has key 4), but the first element is inserted into a new array key 0.
Here's my full code:
$quarter = substr($date, 1,1);
$year = substr($date, 2,4);
$voters = get_post_meta(2165, 'bonus_votesq'. $quarter . $year);
//initialize vote arrays and vars
$votes = $_POST['votes'];
$voter_id = $_POST['voter_id'];
$voting_array = array();
$current_vote = array();
parse_str($votes, $voting_array);
foreach ($voting_array as $vid => $awarded_points) {
$current_vote[sanitize_key($vid)] = sanitize_key($awarded_points);
}
// push the local array into what will be the global array
$voters[$voter_id] = $current_vote;
//if meta_data doesn't exist, update_post_meta creates one. if it does, it updates it.
update_post_meta(2165,'bonus_votesq'. $quarter . $year, $voters);
echo print_r($voters);
What is going on?