foreach()检查以前是否是相同的ID

时间:2014-08-21 08:47:19

标签: php foreach

我有以下代码:

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

3 个答案:

答案 0 :(得分:0)

试试这个

$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;
        }
    }
}

答案 1 :(得分:0)

试试这个

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/>";
}

答案 2 :(得分:0)

这可能有效:

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;
        }
    }
}