doucheng1063 2012-07-08 20:13 采纳率: 100%
浏览 134
已采纳

如何为数组中的每个插入项提供唯一ID?

I run a situation where I put stuff into array like that:

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

How can I mark each entry with its own unique ID so I can grab it later?

Thanks

  • 写回答

1条回答 默认 最新

  • dreamevil0002 2012-07-08 20:17
    关注

    You can always self-assign incremental numbers, that's one option.

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

    Or you can just use any key/value pairs.

    $array['id'] = ...
    $array[123]  = ...
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?