PHP 根据一个字段的值将数组分成几组
我有一个数组,其中包含按字母顺序排列的名称和其他详细信息的数组。每个数组都包含与名称关联的第一个字母。
Array
(
[0] => Array
(
[0] => a
[1] => Alanis Morissette
)
[1] => Array
(
[0] => a
[1] => Alesha Dixon
)
[2] => Array
(
[0] => a
[1] => Alexandra Burke
)
[3] => Array
(
[0] => b
[1] => Britney Spears
)
[4] => Array
(
[0] => b
[1] => Bryan Adams
)
)
我想按第一个首字母分组显示它们,例如:
A - Alanis Morissette Alesha Dixon Alexandra Burke B - Britney Spears Bryan Adams etc...
这可能吗?
I have an array containing arrays of names and other details, in alphabetical order. Each array includes the first letter associated with the name.
Array
(
[0] => Array
(
[0] => a
[1] => Alanis Morissette
)
[1] => Array
(
[0] => a
[1] => Alesha Dixon
)
[2] => Array
(
[0] => a
[1] => Alexandra Burke
)
[3] => Array
(
[0] => b
[1] => Britney Spears
)
[4] => Array
(
[0] => b
[1] => Bryan Adams
)
)
I'd like to display them grouped by that first initial, eg:
A - Alanis Morissette Alesha Dixon Alexandra Burke B - Britney Spears Bryan Adams etc...
Is this at all possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使它们没有排序,您也可以轻松地对它们进行分组:
您甚至不需要存储第一个首字母来对它们进行分组:
You can group them easily, even if they aren't sorted:
You don't even need to store the first initial to group them:
由于您的数组已经排序,您可以循环遍历并跟踪显示的最后一个字母。当它发生变化时,您就知道您正在写下一封信。
Since your array is already sorted, you could just loop through and track the last letter shown. When it changes, you know you're on the next letter.