foreach()检查前一个ID是否相同


foreach() checking if previous was same ID

我有以下代码:

foreach ( $r2->{$id}->pages as $page ) {
    echo "<br>".$page->name.":<br>";
    foreach ( $page->slots as $slot ) {
        $id = $slot->runeId;
        $details = $result->$id->{'name'};
        echo $slot->runeSlotId.": ".$slot->runeId.": ".$details;
        echo "<br>";
    }
}

输出类似于:

AD ArmorHP Mix:
1: 5245: Greater Mark of Attack Damage
2: 5245: Greater Mark of Attack Damage
3: 5245: Greater Mark of Attack Damage
4: 5245: Greater Mark of Attack Damage
5: 5245: Greater Mark of Attack Damage
6: 5245: Greater Mark of Attack Damage
7: 5245: Greater Mark of Attack Damage
8: 5245: Greater Mark of Attack Damage
9: 5245: Greater Mark of Attack Damage
10: 5317: Greater Seal of Armor
11: 5317: Greater Seal of Armor
12: 5317: Greater Seal of Armor
13: 5317: Greater Seal of Armor
14: 5316: Greater Seal of Scaling Health
15: 5316: Greater Seal of Scaling Health
16: 5316: Greater Seal of Scaling Health
17: 5316: Greater Seal of Scaling Health
18: 5316: Greater Seal of Scaling Health
19: 5290: Greater Glyph of Scaling Magic Resist
20: 5290: Greater Glyph of Scaling Magic Resist
21: 5290: Greater Glyph of Scaling Magic Resist
22: 5290: Greater Glyph of Scaling Magic Resist
23: 5290: Greater Glyph of Scaling Magic Resist
24: 5290: Greater Glyph of Scaling Magic Resist
25: 5290: Greater Glyph of Scaling Magic Resist
26: 5290: Greater Glyph of Scaling Magic Resist
27: 5290: Greater Glyph of Scaling Magic Resist
28: 5335: Greater Quintessence of Attack Damage
29: 5335: Greater Quintessence of Attack Damage
30: 5335: Greater Quintessence of Attack Damage
...
1: ...

我该如何检查$slot->runeId的先前foreach()输出是否与这次输出的相同,然后将x1 ... x2 ... x3等添加到字符串的末尾?

的例子:

1: 5245: Greater Mark of Attack Damage
2: 5245: Greater Mark of Attack Damage
3: 5245: Greater Mark of Attack Damage
4: 5245: Greater Mark of Attack Damage
5: 5245: Greater Mark of Attack Damage
6: 5245: Greater Mark of Attack Damage
7: 5245: Greater Mark of Attack Damage
8: 5245: Greater Mark of Attack Damage
9: 5245: Greater Mark of Attack Damage
->
1: 5245: Greater Mark of Attack Damage x9

try this

foreach ( $r2->{$id}->pages as $page ) {
    $prev_id = "";
    $prev_count = 0;
    echo "<br>".$page->name.":<br>";
    foreach ( $page->slots as $slot ) {
        $id = $slot->runeId;
        $details = $result->$id->{'name'};
        if($prev_id!=$id)
        {
            if($prev_id!="")
            {
                echo " x".$prev_count;
                echo "<br/>";
                $prev_count = 0;
            }
            echo $slot->runeSlotId.": ".$slot->runeId.": ".$details;
        }
        $prev_count += 1;
        $prev_id = $id;
    }
    echo " x".$prev_count;
    echo "<br/>";
}

试试这个

$lastId = 0;
$counter = 1;
foreach ( $r2->{$id}->pages as $page ) {
    echo "<br>".$page->name.":<br>";
    foreach ( $page->slots as $slot ) {
        $id = $slot->runeId;
        $details = $result->$id->{'name'};
        echo $slot->runeSlotId.": ".$slot->runeId.": ".$details.(($count==0)?(''):(' x'.$count));
        echo "<br>";
        if ($slot->runeId == $lastId) {
           $counter++;
        } else {
           $lastId = $slot->runeId;
           $counter = 1;
        }
    }
}

这可能行得通:

foreach ( $r2->{$id}->pages as $page ) {
    echo "<br>".$page->name.":<br>";
    $tempSlot; $tempOut;
    $count = 0;
    foreach ( $page->slots as $slot ) {
        $id = $slot->runeId;
        $details = $result->$id->{'name'};
        $tempSlot =  $slot->runeSlotId.": ".$slot->runeId.": ".$details;
        if ($count == 0) {
            $tempOut == $tempSlot;
        }
        if ($tempSlot == $tempOut) {
            $count++;
        } else {
            echo $tempSlot;
            if ($count > 1) {
                echo " x".$count;
            }
            echo '<br>';
            $count = 0;
        }
    }
}