PHP 检查重复的连续值

发布于 2024-10-07 05:42:05 字数 384 浏览 0 评论 0原文

我正在 PHP 中显示一些数据,并且正在寻找逻辑来检查重复值,但仅限于连续的重复值。

例如,现在我显示了音乐会上演奏的音乐列表:

贝多芬第九交响曲
- 第 1 部分

贝多芬第九交响曲
- 第二部分

马勒交响曲
- 第 1 部分

贝多芬第九交响曲
- 第 4 部分

相反,我希望它读起来像这样 - 这样,如果标题与上一个项目的标题匹配,我就不会重复该标题。但如果它与之前的项目不同,它会再次显示标题。

贝多芬第九交响曲
- 第 1 部分
- 第二部分

马勒交响曲
- 第 1 部分

贝多芬第九交响曲
- 第 4 部分

感谢您的帮助!

I am displaying some data in PHP and I'm looking for logic to check for duplicate values, but only sequential duplicate values.

For example, right now I have this showing up for a list of music played at a concert:

Beethoven Ninth Symphony
- Part 1

Beethoven Ninth Symphony
- Part 2

Mahler Symphony
- Part 1

Beethoven Ninth Symphony
- Part 4

Instead I'd like it to read like this - so that I don't repeat the title IF it matches the title of the previous item. But if it's not the same as the item before it, it would display the title again.

Beethoven Ninth Symphony
- Part 1
- Part 2

Mahler Symphony
- Part 1

Beethoven Ninth Symphony
- Part 4

Thanks for any help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

逐鹿 2024-10-14 05:42:05

假设您有一个交响乐对象列表 $musicList。每个项目都有标题和零件号属性。

$previousTitle = 0;
foreach ($musicList as $item) {
  if ($item->title != $previousTitle) {
    print $item->title;
  }
  print "- " . $item->partNumber;

  $previousTitle = $item->title;
}

Suppose you have a list of symphony objects, $musicList. Each item has title and part number properties.

$previousTitle = 0;
foreach ($musicList as $item) {
  if ($item->title != $previousTitle) {
    print $item->title;
  }
  print "- " . $item->partNumber;

  $previousTitle = $item->title;
}
寂寞清仓 2024-10-14 05:42:05

您只需将打印的最后一个音乐会名称存储在某个变量中,以便在打印每个音乐会名称时可以将其与当前音乐会名称进行比较。当它们相等时,不打印任何内容,当它们不相等时,更新变量并打印新的音乐会名称。

while ($row = mysql_fetch_array($result_resource)) {

    if (!isset($last) || $last != $row['concert_name']) {
        $last = $row['concert_name'];
        echo "<h1>" . $row['concert_name'] . "</h1>";
    }
    echo $row['song_name'] . "<br />";

}

You just need to store the last concert name you printed in some variable, so that you can compare it to the current concert name as you print out each one. When they are equal, don't print anything, when they're not, update the variable and print the new concert name.

while ($row = mysql_fetch_array($result_resource)) {

    if (!isset($last) || $last != $row['concert_name']) {
        $last = $row['concert_name'];
        echo "<h1>" . $row['concert_name'] . "</h1>";
    }
    echo $row['song_name'] . "<br />";

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文