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:

<?php foreach ($quotes as $quote) : ?>
   <?php echo $block->escapeHtml($quote->getStatusLabel()) ?>
<?php endforeach; ?>

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

$stopon=['Open','Closed'];
foreach( $quotes as $quote ){
    if( in_array( $quote->getStatusLabel(), $stopon ) ){
        echo 'found - '.$quote->getStatusLabel();
        break;
    }
}

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.

# some dummy data to populate a $quotes array of object instances
$rs=array(
    array("id"=>"4","quote"=>"11015.684616","label"=>"Pending"),
    array("id"=>"7","quote"=>"11015.684616","label"=>"Pending"),
    array("id"=>"8","quote"=>"11154.335600","label"=>"Closed"),
    array("id"=>"1","quote"=>"11015.684616","label"=>"Open"),
    array("id"=>"2","quote"=>"11154.335600","label"=>"Closed"),
    array("id"=>"3","quote"=>"11163.040000","label"=>"Open"),
    array("id"=>"5","quote"=>"11154.335600","label"=>"Open"),
    array("id"=>"6","quote"=>"11133.720000","label"=>"Open"),
    array("id"=>"9","quote"=>"11163.040000","label"=>"Open"),
    array("id"=>"10","quote"=>"11015.684616","label"=>"Closed")
);


/* example to emulate whatever class $quote is... */
class quote{
    private $data;
    public function __construct($data=array()){
        $this->data=$data;
    }
    public function getStatusLabel(){
        return $this->data['label'];
    }
    public function getQuoteValue(){
        return $this->data['quote'];
    }
    public function getid(){
        return $this->data['id'];
    }
}       




/*
    to emulate a similar data structure to that depicted
    in the question.
*/
$quotes=array();
foreach( $rs as $a )$quotes[]=new quote( $a );




$stopon=['Open','Closed'];
$total=0;
foreach( $quotes as $quote ){
    $total++;
    if( in_array( $quote->getStatusLabel(), $stopon ) ){
        break;
    }
}
echo '<br />Total records until match made: '.$total;
echo '<br />Total number of records in source: '.count( $quotes );



$stopon=['Open','Closed'];
$total=0;
foreach( $quotes as $quote ){
    $total += $quote->getQuoteValue();
    if( in_array( $quote->getStatusLabel(), $stopon ) ){
        break;
    }
}
echo '<br />Total Quote Value until match made: '.$total;


$stopon=['Open','Closed'];
$total=0;
foreach( $quotes as $quote ){
    $total += $quote->getQuoteValue();
}
echo '<br />Absolute Quote Value total: '.$total;

The output from the above would be:

Total records until match made: 3
Total number of records in source: 10
Total Quote Value until match made: 33185.704832
Absolute Quote Value total: 110985.545264
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement