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

[eluser]vtatds[/eluser]
In my view, I'm doing a foreach loop:
foreach($results as $result){
if ($result == 'noresults'){echo 'There are no results';}
else{echo $result->activity_name;}

in the else, i want to check if the next element in the array has the same value as the current. I need to write a div prior to it if it has the same value (to hide it for an accordian), and not write a div if it does not have the same value.

i feel like i'm missing something simple here. I thought next() would do it but i get a php error if i do next($result->activity_name) stating that php is expecting a string.

any thoughts? need more explanation to make it clearer?
#2

[eluser]Georgi Budinov[/eluser]
Generally for this I would use a for loop. The 'nex't function needs the array itself e.g. next($results) but using it in the foreach loop will mess it up I think.
#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;
        }
    }
}
#4

[eluser]danmontgomery[/eluser]
Worth noting that this only works for numeric indexes. For associative arrays, you might be able to work something out with next() to preview the next element, and prev() to return the pointer to the correct one.
#5

[eluser]vtatds[/eluser]
I kinda see where you're going with this:
Code:
(isset($result[$key+1]) && $result[$key] == $result[$key+1])
however, i get an error (which i can't see for some reason, the page just doesn't process). when i comment out the if structure and just output the $key and $result->activity_name though. i just get the position of the element in the array and the activity_name.

I'm trying to check if that current activity_name and the next activity_name are the same. not sure what i'm missing.
#6

[eluser]vtatds[/eluser]
sorry. i think i didn't appropriately state the question. the array is an associative array.
Code:
array(4)
{
    [0]=>  object(stdClass)#18 (10)
    {
        ["activity_name"]=>  string(4) "Bowling"
        ["notes"]=>  string(20) "these are some notes"
    }
    [1]=>  object(stdClass)#19 (10)
    {
        ["activity_name"]=>  string(4) "Bowling"
        ["notes"]=>  NULL
    }
    [2]=>  object(stdClass)#20 (10)
    {
        ["activity_name"]=>  string(4) "Golf"
        ["notes"]=>  NULL
    }
    [3]=>  object(stdClass)#21 (10)
    {
        ["activity_name"]=>  string(5) "Tennis"
        ["notes"]=>  NULL
    }      
}
i'm trying to find out if array(1) has the same value for activity_name as array(0) when i'm on array(0) and so on till the end of the array




Theme © iAndrew 2016 - Forum software by © MyBB