简体   繁体   中英

looping through an array and breaking when switch statement condition is met

I am attempting to loop through an array of arrays, and for each array, I am running a function. This function has a switch statement that will alert the user if a condition is met. I want the loop to break instantly when an error is found (not 'good') Unfortunately, because this function is ran with each item in the array, the loop will only count the last item's condition.

Here is where I am looping through the array:

foreach($Array as $item){
        
    $Id = $item['data1'];
    $name = $item['data2'];
    $age = $item['data3'];

    if(!$Id){
        $con = 'id';
        errorHandler($con); 
    }else if (!$name){
        $con = 'name';
        errorHandler($con); 
    }else if (!age){
        $con = 'age';
        errorHandler($con); 
    }else{
        $con = 'good';
        errorHandler($con); 
    }

Here is the errorHandler function


function errorHandler($con){

    switch($con){
    case 'id':
        $body = 'there was no id'
              break;
    case 'name':
        $body = 'there was no name'
              break;
    case 'age':
        $body = 'there was no age'
              break;
    case 'age':
        $body = 'all good'
              break;
        ?>
<h1><?php echo $body ?></h1>
<?php
    }

So basically as it loops through the array if the last item in the array is equal to the final else condition ('good'), the 'all good' will be displayed, no matter what the other items were. Basically what is displayed to the user is whatever the condition of last item in the array is. I want the foreach to loop through the array, and instantly break if any condition other than 'good' is met, and show the user the error.

The loop shouldn't even get to the last item if anything other than 'good' is met.

I think it's to do with the function being called for every item in the loop but I can't figure it out.

How can I fix my code so that if the user hits any condition other than the 'good', the switch statement shows the error and stops the loop?

errorHandler() should return a boolean that indicates whether there was an error. Then you can check this in foreach and break out of the loop.

function errorHandler($con){
    $body = '';
    switch($con){
    case 'id':
        $body = 'there was no id';
        break;
    case 'name':
        $body = 'there was no name';
        break;
    case 'age':
        $body = 'there was no age';
        break;
    case 'age':
        $body = 'all good';
        break;
    }
    ?>
<h1><?php echo $body ?></h1>
<?php
    return $body != '';
}

foreach($Array as $item){
        
    $Id = $item['data1'];
    $name = $item['data2'];
    $age = $item['data3'];

    if(!$Id){
        $con = 'id';
        if (errorHandler($con)) {
            break;
        }; 
    }else if (!$name){
        $con = 'name';
        if (errorHandler($con)) {
            break;
        }; 
    }else if (!age){
        $con = 'age';
        if (errorHandler($con)) {
            break;
        }; 
    }else{
        $con = 'good';
        if (errorHandler($con)) {
            break;
        }; 
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM