3

There are three foreach loop and on the last foreach loop there is an if condition,

foreach ($candidate_data as $cd) {
        $edu_data=DB::table('applicant_edu_info')
        ->where('applicant_id',$cd->ap_id)
        ->get();
            foreach($edu_data as $ed){
                foreach($neededDegree as $nd){
                    if($neededDegree->edu_degree_id==$ed->edu_degree_id){
                        $education_data[$cd->ap_id]=$neededDegree->name;

                    }
                }
            }
    }

what I need is , If the condition is true then I want to break two loops and continue running from the first foreach loop. Please help.

1
  • You should find a way to simplify your nested loops. Querying every loop is such a waste of memory. Why not do an IN statement instead? Commented Jan 23, 2018 at 5:35

2 Answers 2

7

In PHP you can use the optional param of break.

Use break 2;

foreach ($candidate_data as $cd) {
        $edu_data=DB::table('applicant_edu_info')
        ->where('applicant_id',$cd->ap_id)
        ->get();
            foreach($edu_data as $ed){
                foreach($neededDegree as $nd){
                    if($neededDegree->edu_degree_id==$ed->edu_degree_id){
                        $education_data[$cd->ap_id]=$neededDegree->name;
                         break 2;

                    }
                }
            }
    }

break 2 will break two loops.

Docs: http://php.net/manual/en/control-structures.break.php

Sign up to request clarification or add additional context in comments.

Comments

1

You can use goto which used to jump to another section in the program.

foreach ($candidate_data as $cd) {
    $edu_data=DB::table('applicant_edu_info')
    ->where('applicant_id',$cd->ap_id)
    ->get();
        foreach($edu_data as $ed){
            foreach($neededDegree as $nd){
                if($neededDegree->edu_degree_id==$ed->edu_degree_id){
                    $education_data[$cd->ap_id]=$neededDegree->name;
                    goto targetLocation;

                }
            }
        }
      targetLocation:
}

Using goto you can set your target anywhere you want.

1 Comment

that one is pretty good one but in case of specific location it may help too

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.