Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将元素插入数组会在它们不在的位置创建嵌套数组';我不想要_Php_Arrays_Wordpress - Fatal编程技术网

Php 将元素插入数组会在它们不在的位置创建嵌套数组';我不想要

Php 将元素插入数组会在它们不在的位置创建嵌套数组';我不想要,php,arrays,wordpress,Php,Arrays,Wordpress,我正在WP中存储post_meta_密钥。该值是一个数组 我做了一个get_post_meta-$single设置为false。此数组称为$vorters,因为它存储投票者列表。键是用户id,值是数组,如下所述 然后我循环输入,对它们进行清理,并创建另一个键=>值数组,称为current\u vote。这是设置为用户id键的值 我想将current_vote作为一个值插入选民数组。现在,我正在尝试: $voters[$voter_id] = $current_vote; 这将正确地从$vote

我正在WP中存储post_meta_密钥。该值是一个数组

我做了一个get_post_meta-<代码>$single设置为false。此数组称为
$vorters
,因为它存储投票者列表。键是用户id,值是数组,如下所述

然后我循环输入,对它们进行清理,并创建另一个键=>值数组,称为
current\u vote
。这是设置为用户id键的值

我想将
current_vote
作为一个值插入选民数组。现在,我正在尝试:

$voters[$voter_id] = $current_vote;
这将正确地从
$voter\u id
创建一个键,并将
当前\u 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
        )

)
第二次投票后,情况如下:

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

发生了什么事?

您正在获取post元数据并将它们放回同一数组,
$vorters
。在使用
post\u meta\u data
之前,您应该提前分配
$voter\u id
,并将其用作键,或者使用其他变量来存储它

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

我明白了!杰森问了一个问题——在get_post_meta让我走上正轨后,$voctors看起来像什么


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

变量$voter\u id和$vid是否都表示“当前选民id号”?函数sanitize_key()是否对键、值或两者进行清理?您应该在
get_post_meta
之后立即导出变量($vorters,true),这样您就可以看到每次返回的内容。这一行不是我想要的:$vorters[$vorter_id]=get_post_meta(2165,'bonus_votesq.$quarter.$year);-我想要的是完整的用户列表(进入数组$VOLTERS),然后我想要在$VOLTERS中插入一个新的键/值。如果我这样做,它将只将所有投票者的数组插入一个键--$VOLTER_id键。如果我错了,请纠正我,它看起来像是您正在将post(表单)数据拉入
$VOLTERS
,重新格式化,然后将其放回
$VOLTERS
?关闭。我正在将现有数据拉入$VOLTERS,为post['VOLTES']准备$current\u VOLTER中的新数据,并将新数据插入$VOLTERS。在第二次投票调用
get\u post\u meta()
后,
print\r($VOLTERS)
看起来像什么?