从循环外的foreach循环内的东西创建一个If语句?

Im trying to NOT show a specific field inside the loop, so I need to get a list of all the field types so I can use it inside the if statement. Not sure how I can do this properly?

foreach($this->sections as $k => $section){

    foreach($section['fields'] as $k => $type){

        //This makes a nice list of all the stuff I need
        echo '<li>'.var_dump ($type['type']).'</li>';
    }     
        //Outside the loop doesn't dump all of the contents just some  
        echo '<li>'.var_dump ($type['type']).'</li>';

    if($type['type'] != 'switch'){

        //My stuff

    }

}

The idea is to loop all the field types, except for one specific type declared in the if statement. The for each is so I can get a list of all the field types.

As you might already experienced the construct you propose is not desired since the if statement will be executed after the loop has already ended.


You can use the continue keyword to jump to the next iteration and skip fields you're not interested in.

foreach ($section['fields'] as $k => $type) {
    if ($type['type'] != 'switch') {
        continue;
    }

    // do stuff
}

http://php.net/manual/en/control-structures.continue.php