根据名字和姓氏数组生成简短的唯一用户名
我有一个名为 users 的多维数组,其格式如下,我正在尝试创建一个脚本,该脚本将根据以下信息创建“用户名”:
$users = [
['first_name' => 'Bob', 'last_name' => 'Smith'],
['first_name' => 'Steve', 'last_name' => 'Little'],
['first_name' => 'Eric', 'last_name' => 'Fielder'],
['first_name' => 'Steve', 'last_name' => 'Richardson'],
['first_name' => 'Bob', 'last_name' => 'Sanders'],
['first_name' => 'Bob', 'last_name' => 'Sanders'],
['first_name' => 'Bob', 'last_name' => 'Smith'],
];
所需逻辑:
如果它使用的名字没有重复仅名字作为用户名 (“埃里克”)。
如果两个名字的名字首字母相同,但姓氏首字母不同,则会使用名字作为姓氏首字母(“Steve L.”和“Steve R. ”)。
如果多个人都具有姓氏和姓氏首字母,则返回全名(“Bob Smith”和“Bob Sanders”)。
最后,如果找到相同的确切名称,则会为每个名称附加一个数字,如下所示:“Bob Sanders (1)”和“Bob Sanders (2)” >”
”我希望这可以有效地完成,而不需要很多循环,但我一生都无法弄清楚。
I have a multi-dimensional array called users that is formatted like the following and I am trying to create a script that will create a "username" based on this information:
$users = [
['first_name' => 'Bob', 'last_name' => 'Smith'],
['first_name' => 'Steve', 'last_name' => 'Little'],
['first_name' => 'Eric', 'last_name' => 'Fielder'],
['first_name' => 'Steve', 'last_name' => 'Richardson'],
['first_name' => 'Bob', 'last_name' => 'Sanders'],
['first_name' => 'Bob', 'last_name' => 'Sanders'],
['first_name' => 'Bob', 'last_name' => 'Smith'],
];
Required Logic:
If there are no duplicate first names it uses only the first name as the username
("Eric").If there are two names with the same first but different initial letters in the last name it will use the first name as last initial ("Steve L." and "Steve R.").
If multiple people have the last first name and last initial then it return the full name ("Bob Smith" and "Bob Sanders").
Lastly, if the SAME exact name is found then it will append a number to each like this: "Bob Sanders (1)" and "Bob Sanders (2)"
I am hoping this can be done efficiently and not with a lot of loops, but I can not figure it out for the life of me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这个脚本不是那么漂亮,但几乎可以满足您的要求。请注意,它仅使用两个循环,但需要一些额外的内存来存储有关用户的元数据:
This script is not that nifty but pretty much does what you want. Note that it only uses two loops but needs some additional memory to store meta data about the users:
我发现 Nayru 的代码片段太宽,难以理解,而且内存成本太高——为了方便查找,它存储了多余的计数。值得赞扬的是,它确实维护了行的顺序——如果这很重要的话。
另一种技术是将输入数据合并到嵌套组中(具有唯一的级别/键),然后迭代这些合并的级别并使用一系列条件来生成所需的用户名。这可能是跟踪名称冲突的最紧凑的方法。我当然觉得这是一段更容易维护和阅读的代码。
*如果您的姓氏可能以多字节字符开头,则应使用
mb_substr()
来隔离第一个字母*此代码片段的结果不尊重输入的原始顺序,但如有必要,可以为此目的进行重构。
*它确实使用了多个循环,但这只是迭代嵌套级别的最有效方法——不要回避。
代码:(演示)
输出:
I find Nayru's snippet to be excessively wide, hard to follow, and too expensive in terms of memory -- it is storing redundant tallies for the sake of easy lookups. To its credit, it does maintain the order of the rows -- if that matters.
Another technique, is to consolidate the input data into nested groups (with unique levels/keys), then iterate those consolidated levels and use a battery of conditions to generate the desired usernames. This might be the most compact way to track the name collisions. I certainly feel that this is much easier piece of code to maintain and read.
*if your last names might start with a multibyte character, then
mb_substr()
shoul be used to isolate the first letter*the result of this snippet does not respect the original order of the input, but it could be refactored for this purpose if necessary.
*it does use several loops, but this is just the most efficient means to iterating the nested levels -- not to be shied away from.
Code: (Demo)
Output: