我在WP中存储了post_meta_key。值是一个数组。
我做了一个get_post_meta - which returns an empty array the first time。 $single
设置为false。这个数组称为$voters
,因为它存储了一个选民列表。关键是用户id,值是一个数组,接下来描述。
然后我遍历输入,清理它们,并创建另一个名为current_vote
的key =>值数组。这是为用户标识密钥设置的值。
我想将current_vote
作为值插入到投手数组中。现在,我正在尝试这个:
$voters[$voter_id] = $current_vote;
这可以从$voter_id
正确创建一个密钥,并将current_vote
数组作为值。
BUT!当第二次投票进来时,它不只是在现有数组中插入另一个键 - 它需要第一次投票,并将其插入新数组!
第一次投票如下:
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
)
)
第二次投票结束后,看起来像这样:
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
)
)
因此,元素被正确插入(新元素具有键4),但第一个元素被插入到新的数组键0中。
这是我的完整代码:
$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);
发生了什么事?
答案 0 :(得分:0)
您正在获取帖子元数据,并将它们放回到同一个数组$voters
上。您应该提前分配$voter_id
并在使用post_meta_data
之前将其用作密钥,或者使用其他变量来存储它。
如果您的意思是提前使用$voter_id
作为密钥,请尝试过:
//initialize vote arrays and vars
$quarter = substr($date, 1,1);
$year = substr($date, 2,4);
$votes = $_POST['votes'];
$voter_id = $_POST['voter_id'];
$voting_array = array();
$current_vote = array();
$voters[$voter_id] = get_post_meta(2165, 'bonus_votesq'. $quarter . $year);
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);
答案 1 :(得分:0)
我明白了!杰森问的问题 - 在get_post_meta让我走上正轨之后,美元选民的样子是什么样的 -
http://codex.wordpress.org/Function_Reference/get_post_meta
如果将$ single设置为false,那么它就是 - get_post_meta返回一个ASSOCIATIVE ARRAY,其中包含我的结果。所以,这就是额外数组的用武之地。因为我在那里插入新的投票,现在它有关联数组和我的新投票。