PHP 根据一个字段的值将数组分成几组

发布于 2024-08-29 06:04:28 字数 746 浏览 8 评论 0原文

我有一个数组,其中包含按字母顺序排列的名称和其他详细信息的数组。每个数组都包含与名称关联的第一个字母。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南渊 2024-09-05 06:04:28

即使它们没有排序,您也可以轻松地对它们进行分组:

$groups=array();
foreach ($names as $name) {
    $groups[$name[0]][] = $name[1];
}

您甚至不需要存储第一个首字母来对它们进行分组:

$names = array(
  'Alanis Morissette',
  'Alesha Dixon',
  'Alexandra Burke',
  'Britney Spears',
  'Bryan Adams',
  ...
);

$groups=array();
foreach ($names as $name) {
    $groups[$name[0]][] = $name;
}

You can group them easily, even if they aren't sorted:

$groups=array();
foreach ($names as $name) {
    $groups[$name[0]][] = $name[1];
}

You don't even need to store the first initial to group them:

$names = array(
  'Alanis Morissette',
  'Alesha Dixon',
  'Alexandra Burke',
  'Britney Spears',
  'Bryan Adams',
  ...
);

$groups=array();
foreach ($names as $name) {
    $groups[$name[0]][] = $name;
}
轻拂→两袖风尘 2024-09-05 06:04:28

由于您的数组已经排序,您可以循环遍历并跟踪显示的最后一个字母。当它发生变化时,您就知道您正在写下一封信。

$lastChar = '';
foreach($singers as $s) {
    if ($s[0] != $lastChar) echo "\n".$s[0]."\n - \n";
    echo $s[1]."\n";
    $lastChar = $s[0];
} 

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.

$lastChar = '';
foreach($singers as $s) {
    if ($s[0] != $lastChar) echo "\n".$s[0]."\n - \n";
    echo $s[1]."\n";
    $lastChar = $s[0];
} 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文