Reputation: 71
I am working with laravel and trying to get value of an array after another value using foreach loop. I am wondering what would be the best practice in solving this problem. So far I have tried this
$problems = $request->session()->get('problems');
$i = 0;
foreach ($problems as $prob) {
$problem = $prob;
if($request->problem_id == $prob->id){
return 'Matched';
die();
}
$i++;
}
Upvotes: 0
Views: 827
Reputation: 1332
Eventhough @Lucarnosky code will work theres a faster way of handling this.
$problems = array_flip($request->session()->get('problems'));
if(key_exists($request->problem_id, $problems) return 'matched';
return 'problem not found';
Upvotes: 1
Reputation: 514
Change your foreach in something like that
$shouldQuit = false;
foreach ($problems as $prob) {
$problem = $prob;
if($shouldQuit)
break;
if($request->problem_id == $prob->id){
$shouldQuit = true;
}
}
Upvotes: 4