dsvjw20866 2017-11-28 00:40
浏览 42
已采纳

尝试将新的key => value对添加到数组,但它似乎创建并嵌套新数组

I'm trying to add new key => value pairs to an array that I have. The keys are integers that correspond to user IDs. The values would initially just be the string "requested".

The array in question is stored in serialized form at index zero of another array users_attending in a larger array of metadata related to the post that I am modifying.

My process is like this:

  1. Pull a variable that might contain the array from a certain WordPress function. If there's no array then that variable is just empty, we wouldn't see an error.
  2. If the array variable is non-empty and does not contain a key that is equal to the user's ID, then add the key => value pair.
  3. If the array variable is non-empty and does contain a key that is equal to the user's ID, then print that the user is already there.
  4. In all other situations (meaning when the array variable is empty) then create a new array and add the first key => value pair.

However, via this process I see all newly added arrays nesting within first array, then the second array, and so on. When I test the contents of the array with print_r($eventUsers), I see this kind of output:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [4] => requested
                )

            [4] => requested
        )

    [4] => requested
)

For context, here is how users_attending section looks in the larger array of all post metadata (it is automatically serialized by the update_post_meta() function):

[users_attending] => Array
        (
            [0] => a:2:{i:0;a:2:{i:0;a:1:{i:4;s:9:"requested";}i:4;s:9:"requested";}i:4;s:9:"requested";}
        )

Here's the relevant code:

$currentUser = get_current_user_ID(); //let's say this is 4 for now
$eventUsers = get_post_meta(get_the_ID(), 'users_attending');
if (!empty($eventUsers) && !array_key_exists($currentUser, $eventUsers)) {
    $eventUsers[$currentUser] = "requested";
    update_post_meta(get_the_ID(), 'users_attending', $eventUsers);
    //array exists but user is not yet attending
} elseif (!empty($eventUsers) && array_key_exists($currentUser, $eventUsers)) {
    //user is already attending
} else {
    $eventUsers = array();
    $eventUsers[$currentUser] = "requested";
    update_post_meta(get_the_ID(), 'users_attending', $eventUsers); //note that this adds the metadata key and value if it's not already there
    //first user attending
}

What can I do to make sure that the key => value pairs are being added to the first array within the zero index of users_attending instead of this nesting behavior that I am seeing?

  • 写回答

1条回答 默认 最新

  • doushi5024 2017-11-28 01:04
    关注

    The program looks ok, try deleting the data from previous runs.

    Also consider the function get_post_meta maybe returns an array of arrays instead of the array you want.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记