如何为数组中的每个插入项指定一个唯一的ID


How to give a unique ID to each inserted item in array?

我运行这样一种情况,我把东西放入数组中:

    $current_pms[] = array(
        'from' => sofa_get_username($current_user->ID),
        'to' => $valid_user,
        'subject' => $subject,
        'message' => $message,
        'status' => 'unread',
        'time' => current_time('mysql', $gmt = 0)
    );

我如何用自己唯一的ID标记每个条目,以便稍后获取它?

感谢

您始终可以自行分配递增数字,这是一种选择。

$id = count($array)+1;
$array[$id] = array (...)

或者,您可以只使用任何键/值对。

$array['id'] = ...
$array[123]  = ...