Welcome Guest, Not a member yet? Register   Sign In
finding value of next element in an array
#3

[eluser]Jamie Rumbelow[/eluser]
In your foreach loop, you can access the key of the element you're currently on. Doing this, you can increment it to find the next value. Remember to check that it exists though - otherwise PHP will throw an error when you get to the end of the array!

Code:
foreach($results as $key => $result) {
    if ($result == ‘noresults’) {
        echo ‘There are no results’;
    } else {
        if (isset($results[$key+1]) && $results[$key] == $results[$key+1]) {
            echo '<div class="hide">';
            echo $result->activity_name;
            echo '</div>';
        } else {
            echo $result->activity_name;
        }
    }
}


Messages In This Thread
finding value of next element in an array - by El Forum - 09-08-2010, 08:59 PM
finding value of next element in an array - by El Forum - 09-09-2010, 01:02 AM
finding value of next element in an array - by El Forum - 09-09-2010, 01:08 AM
finding value of next element in an array - by El Forum - 09-09-2010, 09:09 AM
finding value of next element in an array - by El Forum - 09-09-2010, 09:42 AM
finding value of next element in an array - by El Forum - 09-09-2010, 11:02 AM



Theme © iAndrew 2016 - Forum software by © MyBB