inserting elements into array creates nested array where they aren't wanted
Ok, i'll try to explain this best i can:
i'm storing a post_meta_key in WP. the value is an array.
i'm starting up by doing a get_post_meta - which, of course, if it's empty
on the first time, returns an empty array
(http://codex.wordpress.org/Function_Reference/get_post_meta). $single is
set to false. we'll call this array $voters, as this will store the list
of voters (the Key will be the userId, the value is an array - which is
the next thing i'm describing)
i'm then looping through my inputs, sanitizing them, and creating another
key=>value array - this is called the current_vote array - this is the
value.
I would like to insert the current_vote as a value into the voters array.
right now, i've got this: $voters[$voter_id] = $current_vote;
i would think that it would create a key from the $voter_id, and put the
current_vote array as the value, and IT DOES that.
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!
so, 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
)
)
but 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.
and 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 the bleeping F is going on???
No comments:
Post a Comment