Skip to content
Advertisement

Check if any item in foreach got specific value and then stop foreach

I use the following foreach code to load all status labels of quote items:

JavaScript

This value can be “Open”, “Closed”, “Pending” etc.

Now I want to change this foreach to check if any of the $quote items got a statuslabel with the value “Open”, “Closed”.

And if any of these items got a statuslabel value like this, it should stop the foreach from searching.

How can I achieve that?

Advertisement

Answer

You can use break after a logic test to escape from the loop

JavaScript

In followup to your question in the comment regarding counting numbers it wasn’t clear of the exact nature of this count so here are some examples of different counts which I hope might be of help.

JavaScript

The output from the above would be:

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement