0

I have a foreach loop and I need to add a continue if the value has already been echoed. I can't find the right syntax. The loop:

foreach ($results as $result) {
   echo $result->date . "<br />"; 
}

But I need to add in a continue so that if the value has already been echoed, and it comes up again in the loop it gets skipped. I can't quite get the if/continue statement quite right.

Thoughts, suggestions, ideas?

4
  • 5
    Typically you would add the date to an array as you echo each one and if in_array continue. Commented Sep 18, 2014 at 21:18
  • 1
    Depending on the nature of a single $result, $results = array_unique($results) before the loop might also help. Commented Sep 18, 2014 at 21:22
  • 1
    @andy It's an object, you can tell that from the code. Commented Sep 18, 2014 at 21:22
  • @CharlotteDunois I can see that. However, if the object implements __toString() the right way, it would work and result in IMO readable code. Commented Sep 18, 2014 at 21:25

3 Answers 3

5

As mentioned by @JonathanKuhn in the comments - here is how you would run that loop:

$already_echoed = array();
foreach ($results as $result) {
    if (!in_array($result->date, $already_echoed)) { //check if current date is in the already_echoed array
        echo $result->date . "<br />";   
    }
    $already_echoed[] = $result->date; //store all dates in an array to check against.
}
Sign up to request clarification or add additional context in comments.

9 Comments

Either you forgot to echo the date or implode and echo the array.
Off topic, is $array[] = $adding_item the preferred method of adding to an array? I always use array_push().
@tatorface $array[] preferred - see reference stackoverflow.com/questions/1074059/…
Instead of posting the code here edit your question - it's too difficult to make sense of it in the comment.
@Dan thanks, I did the test on your linked page and it $array[] is by far and away the quicker tool. Thanks for your help.
|
2
$echoedArray = array();
foreach ($results as $result) {
    if (isset($echoedArray[$result->date])) {
        continue;
    }
    echo $result->date . "<br />";
    $echoedArray[$result->date] = true;
}

4 Comments

Be aware. in_array is much slower function rather than check by the key
in_array is not much slower. And this little difference doesn't really matter. 3v4l.org/nQZbc
well, it would matter if you had hundreds thousands of elements
On HHVM-3.1 in_array() is even faster with 1000 elements. (:
1
    $alreadyOutput = array();
    foreach ($results as $result) {
       if(in_array($result->date, $alreadyOutput)){
           continue;
       }
       $alreadyOutput[] = $result->date; 
       echo $result->date . "<br />";
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.