【问题标题】:inserting elements into array creates nested array where they aren't wanted将元素插入数组会在不需要的地方创建嵌套数组
【发布时间】:2013-08-27 20:55:50
【问题描述】:

我在 WP 中存储了一个 post_meta_key。该值是一个数组。

我做了一个 get_post_meta - which returns an empty array the first time$single 设置为假。这个数组被称为$voters,因为它存储了一个选民列表。 key是用户id,value是一个数组,接下来介绍。

然后我遍历输入,清理它们,并创建另一个名为 current_vote 的 key=>value 数组。这是为用户 ID 键设置的值。

我想将current_vote 作为值插入到投票者数组中。现在,我正在尝试这个:

$voters[$voter_id] = $current_vote;

这会正确地从$voter_id 创建一个键,并将current_vote 数组作为值。

但是!随着第二次投票的到来,它不只是将另一个键插入现有数组 - 它接受第一次投票,并将其插入到新数组中!

第一个投票是这样的:

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

发生了什么事?

【问题讨论】:

  • 变量 $voter_id 和 $vid both 是否表示“当前选民 ID 号”? sanitize_key() 函数是否对键、值或两者都进行了清理?
  • 你应该在get_post_meta之后立即var_export($voters, true),这样你每次都能看到你得到了什么。

标签: php arrays wordpress


【解决方案1】:

您正在获取帖子元数据并将它们放回同一数组$voters。您应该在使用post_meta_data 之前提前分配$voter_id 并将其用作键,或者使用不同的变量来存储它。

如果您的意思是提前使用$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);

【讨论】:

  • 这条线不是我想要的:$voters[$voter_id] = get_post_meta(2165, 'bonus_votesq'. $quarter . $year); - 我想要的是完整的用户列表(到数组 $voters 中),然后我想在 $voters 中插入一个新的键/值。如果我这样做,它会将所有选民的数组插入到一个键中 - $voter_id 键。
  • 如果我错了,请纠正我,看起来您正在将 post(Form) 数据拉入$voters,重新格式化,然后将其放回$voters
  • 关闭。我正在将现有数据拉入 $voters,在 $current_vote 中准备来自 post['votes'] 的新数据,并将新数据重新插入 $voters。
  • print_r($voters)get_post_meta() 被要求进行第二次投票之后是什么样子的?
【解决方案2】:

我明白了! Jason 提出的问题 - 在 get_post_meta 让我走上正轨之后,$voters 是什么样的 -

http://codex.wordpress.org/Function_Reference/get_post_meta

如果 $single 设置为 false,它就是 - get_post_meta 返回一个关联数组,其中包含我的结果。所以,这就是额外的数组进来的地方。因为我在那里插入了我的新选票,它现在有了关联数组和我的新选票。

【讨论】:

    猜你喜欢
    • 2015-04-02
    • 2015-08-24
    • 2016-07-06
    • 2021-10-18
    • 1970-01-01
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多