doupai8533 2013-08-27 20:55
浏览 33

将元素插入到数组中会创建不需要它们的嵌套数组

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?

  • 写回答

2条回答 默认 最新

  • douanye8442 2013-08-27 21:33
    关注

    You are getting the post meta data and putting them back onto the same array, $voters. You should either assign the $voter_id ahead of time and use it as the key before you use post_meta_data, or use a different variable to store it.

    If you mean use $voter_id as a key ahead of time, have you tried this:

    //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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 制裁名单20240508芯片厂商
  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接