Sun_Blood Posted August 20, 2010 Share Posted August 20, 2010 I'm tired and want to sleep so my mistakes are getting lots now. Anybody awake that can explain my problem with the code below. <? function directoryToArray($directory, $recursive) { $array_items = array(); $i = "0"; if ($handle = opendir($directory)) { while (false !== ($file = readdir($handle)) && $i < "3") { if ($file != "." && $file != "..") { if (is_dir($directory. "/" . $file)) { if($recursive) { $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive)); } } else { $file = $directory . "/" . $file; $array_items[] = preg_replace("/\/\//si", "/", $file); $i++; } } } closedir($handle); } natsort($array_items); return array_reverse($array_items); } $data = directoryToArray('images/screenshot/', TRUE); print_r($data); ?> What I want is for the while loop to stop after 4 hits and exit and give me the return results. But now it continue until readdir end. Quote Link to comment https://forums.phpfreaks.com/topic/211328-php-dont-stop-executing-after-i-3/ Share on other sites More sharing options...
AtlasC1 Posted August 20, 2010 Share Posted August 20, 2010 Get rid of the "quotes" around your numerical values. See if that helps. -jm Quote Link to comment https://forums.phpfreaks.com/topic/211328-php-dont-stop-executing-after-i-3/#findComment-1101884 Share on other sites More sharing options...
AbraCadaver Posted August 20, 2010 Share Posted August 20, 2010 Four hits of what? Subdirs, files, either one? Quote Link to comment https://forums.phpfreaks.com/topic/211328-php-dont-stop-executing-after-i-3/#findComment-1101887 Share on other sites More sharing options...
Sun_Blood Posted August 20, 2010 Author Share Posted August 20, 2010 Removing quotes did not help. Return is only files. So 4 files. Quote Link to comment https://forums.phpfreaks.com/topic/211328-php-dont-stop-executing-after-i-3/#findComment-1101890 Share on other sites More sharing options...
AbraCadaver Posted August 20, 2010 Share Posted August 20, 2010 Make $i static so it retains its value in the recursive calls: static $i = 0; Quote Link to comment https://forums.phpfreaks.com/topic/211328-php-dont-stop-executing-after-i-3/#findComment-1101891 Share on other sites More sharing options...
AbraCadaver Posted August 20, 2010 Share Posted August 20, 2010 Quote Make $i static so it retains its value in the recursive calls: static $i = 0; Depending upon how you use it that may cause problems, not sure. So you should probably pass in $i like so: function directoryToArray($directory, $recursive, $i=0) { Then remove: static $i = 0; Then in the recursive call do: $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive, $i)); Quote Link to comment https://forums.phpfreaks.com/topic/211328-php-dont-stop-executing-after-i-3/#findComment-1101892 Share on other sites More sharing options...
Sun_Blood Posted August 20, 2010 Author Share Posted August 20, 2010 The static trick worked but not the other one. If I do that I get 5 hits and not 4 and if I lower i=3 then i get 3 hits so some hidden file get counted as 1. Where did you plan to put the $i++ in your second example? Sorry I must sleep now. Hope I can fix all this tomorrow with a fresh brain Thanks for all the suggestions! Quote Link to comment https://forums.phpfreaks.com/topic/211328-php-dont-stop-executing-after-i-3/#findComment-1101897 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.