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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址