you could store them directly after else condition since your using break; you dont have to worry about iteration
foreach($files as $file){
foreach ($folders as $folder_path) {
/* NOTE : the '@' here before the file_exists() function
is to manage this function errors by yourself after the `else` condition ,
otherways you can remove it to see the native php errors .
*/
if (@file_exists($folder_path.$file)) {
$exists[] = $file;
break;
}else{
$notexists[] = $file;
}
}
}
RESULT :
Exist Files : Array ( [0] => index.php [1] => head.php )
Non Exist ones : Array ( [0] => random_file.random )
but be aware that you need to define those arrays before you use them inside the conditions so you dont get undefined errors later if they return an empty array when you loop them as a result .
so before your first foreach() add those variables definition
$exists = array();
$notexists = array();
$folders = array('./'); // your folders list ofc should be here ..
$this_folderinfile_exists()?$folder_pathseems to be the same in all iterations of the secondforeach().elseruns if it's not in the current directory, even though it might be in another directory.