RopeADope Posted March 25, 2011 Share Posted March 25, 2011 Hi all. I've got this bit of code: $needles=array(`.`,`..`,`css`,`php`,`input_forms`); $t1=scandir($_SERVER['DOCUMENT_ROOT']); foreach($t1 as $t1_value){ if(!is_dir($t1_value)){ unset($t1[array_search($t1_value,$t1)]); }else{ unset($t1[array_search($needles,$t1)]); } } As you can see, I'm trying (in the else statement) to array search $t1 for everything in $needles, then unset the matches from $t1. But its not working and I'm not sure why. I also tried... unset($t1[array_search(in_array($needles,$t1),$t1)]); But that doesn't work either. It only removes the "." directory from the array. Any ideas on what's going wrong? Quote Link to comment https://forums.phpfreaks.com/topic/231680-in_array-not-working-trouble-with-needle-array/ Share on other sites More sharing options...
trq Posted March 26, 2011 Share Posted March 26, 2011 In your $needles array, your using backticks instead of quotes. Quote Link to comment https://forums.phpfreaks.com/topic/231680-in_array-not-working-trouble-with-needle-array/#findComment-1192346 Share on other sites More sharing options...
RopeADope Posted March 26, 2011 Author Share Posted March 26, 2011 That was a last ditch effort sort of thing. I've also tried single/double quotes and still no luck. Quote Link to comment https://forums.phpfreaks.com/topic/231680-in_array-not-working-trouble-with-needle-array/#findComment-1192347 Share on other sites More sharing options...
creata.physics Posted March 26, 2011 Share Posted March 26, 2011 Have you tried to dump $tl or $tl_value to see what is stored? var_dump($tl_value); // inside the else statement of course Have you tried to debug your code and find out why it's not matching yet? Quote Link to comment https://forums.phpfreaks.com/topic/231680-in_array-not-working-trouble-with-needle-array/#findComment-1192356 Share on other sites More sharing options...
nethnet Posted March 26, 2011 Share Posted March 26, 2011 The problem is that you are using an array as your needle in the array_search() in your else statement. It is using only the value at key 0 for this (a.k.a your '.' directory). Here's a quick fix: <?php $needles=array('.','..','css','php','input_forms'); $t1=scandir($_SERVER['DOCUMENT_ROOT']); foreach($t1 as $t1_value){ if (!is_dir($t1_value)){ unset($t1[array_search($t1_value,$t1)]); }else if (array_search($t1_value,$needles) !== false){ unset($t1[array_search($t1_value,$t1)]); } } ?> This time, if it's a directory, it is checked against the $needles array, and if it does in fact match, then it is unset from the scandir array. -nethnet Quote Link to comment https://forums.phpfreaks.com/topic/231680-in_array-not-working-trouble-with-needle-array/#findComment-1192358 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.