rick645 Posted February 5 Share Posted February 5 dir$ pwd /tmp/tmp.xWJxJWnltu/dir dir$ tree . ├── a ├── b └── c 0 directories, 3 files dir$ cat ../test.php <?php var_dump( 'OUTPUT1', new RecursiveDirectoryIterator( getcwd() ) ); var_dump( 'OUTPUT2', iterator_to_array( new RecursiveDirectoryIterator( getcwd() ) ) ); var_dump( 'OUTPUT3', iterator_to_array( new RecursiveDirectoryIterator( getcwd(), FilesystemIterator::CURRENT_AS_PATHNAME ) ) ); var_dump( 'OUTPUT4', iterator_to_array( new RecursiveDirectoryIterator( getcwd(), FilesystemIterator::CURRENT_AS_PATHNAME . FilesystemIterator::SKIP_DOTS ) ) ); dir$ php ../test.php /tmp/tmp.xWJxJWnltu/test.php:5: string(7) "OUTPUT1" /tmp/tmp.xWJxJWnltu/test.php:5: class RecursiveDirectoryIterator#1 (4) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/." private $fileName => string(1) "." private $glob => bool(false) private $subPathName => string(0) "" } /tmp/tmp.xWJxJWnltu/test.php:12: string(7) "OUTPUT2" /tmp/tmp.xWJxJWnltu/test.php:12: array(5) { '/tmp/tmp.xWJxJWnltu/dir/.' => class SplFileInfo#3 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/." private $fileName => string(1) "." } '/tmp/tmp.xWJxJWnltu/dir/c' => class SplFileInfo#4 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/c" private $fileName => string(1) "c" } '/tmp/tmp.xWJxJWnltu/dir/..' => class SplFileInfo#5 (2) { private $pathName => string(26) "/tmp/tmp.xWJxJWnltu/dir/.." private $fileName => string(2) ".." } '/tmp/tmp.xWJxJWnltu/dir/b' => class SplFileInfo#6 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/b" private $fileName => string(1) "b" } '/tmp/tmp.xWJxJWnltu/dir/a' => class SplFileInfo#7 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/a" private $fileName => string(1) "a" } } /tmp/tmp.xWJxJWnltu/test.php:21: string(7) "OUTPUT3" /tmp/tmp.xWJxJWnltu/test.php:21: array(5) { '/tmp/tmp.xWJxJWnltu/dir/.' => string(25) "/tmp/tmp.xWJxJWnltu/dir/." '/tmp/tmp.xWJxJWnltu/dir/c' => string(25) "/tmp/tmp.xWJxJWnltu/dir/c" '/tmp/tmp.xWJxJWnltu/dir/..' => string(26) "/tmp/tmp.xWJxJWnltu/dir/.." '/tmp/tmp.xWJxJWnltu/dir/b' => string(25) "/tmp/tmp.xWJxJWnltu/dir/b" '/tmp/tmp.xWJxJWnltu/dir/a' => string(25) "/tmp/tmp.xWJxJWnltu/dir/a" } /tmp/tmp.xWJxJWnltu/test.php:31: string(7) "OUTPUT4" /tmp/tmp.xWJxJWnltu/test.php:31: array(3) { '/tmp/tmp.xWJxJWnltu/dir/c' => class SplFileInfo#5 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/c" private $fileName => string(1) "c" } '/tmp/tmp.xWJxJWnltu/dir/b' => class SplFileInfo#4 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/b" private $fileName => string(1) "b" } '/tmp/tmp.xWJxJWnltu/dir/a' => class SplFileInfo#3 (2) { private $pathName => string(25) "/tmp/tmp.xWJxJWnltu/dir/a" private $fileName => string(1) "a" } } The output that comes closest to my expectations is OUTPUT4 (everything else is to be discarded). But actually I would like something like this array(3) { [0] => string(25) "/tmp/tmp.xWJxJWnltu/dir/c" [1] => string(25) "/tmp/tmp.xWJxJWnltu/dir/b" [2] => string(25) "/tmp/tmp.xWJxJWnltu/dir/a" } The simplest output!!! Is it possible? Quote Link to comment https://forums.phpfreaks.com/topic/317694-directory-file-list/ Share on other sites More sharing options...
requinix Posted February 5 Share Posted February 5 Have you considered, perhaps, writing some code to do that? Quote Link to comment https://forums.phpfreaks.com/topic/317694-directory-file-list/#findComment-1614751 Share on other sites More sharing options...
rick645 Posted February 5 Author Share Posted February 5 I was thinking of doing it with a single instruction Quote Link to comment https://forums.phpfreaks.com/topic/317694-directory-file-list/#findComment-1614757 Share on other sites More sharing options...
Solution requinix Posted February 5 Solution Share Posted February 5 One-liners are great and all, but is it worth all the time you've spent on it so far? A simple foreach loop could have solved the problem hours ago. Quote Link to comment https://forums.phpfreaks.com/topic/317694-directory-file-list/#findComment-1614758 Share on other sites More sharing options...
rick645 Posted February 6 Author Share Posted February 6 For every problem, we always tend to look for the simplest solution $solution1 = 2 * 5; $solution2 = 2 + 2 + 2 + 2 + 2; for ($i = 1; $i <= 5; $i++) { $solution3 = $i * 2 . PHP_EOL; } Which do you prefer? Quote Link to comment https://forums.phpfreaks.com/topic/317694-directory-file-list/#findComment-1614812 Share on other sites More sharing options...
rick645 Posted February 6 Author Share Posted February 6 For every problem, we always tend to look for the simplest solution $solution1 = 2 * 5; $solution2 = 2 + 2 + 2 + 2 + 2; for ($i = 1; $i <= 5; $i++) { $solution3 = $i * 2 . PHP_EOL; } Which do you prefer? Quote Link to comment https://forums.phpfreaks.com/topic/317694-directory-file-list/#findComment-1614813 Share on other sites More sharing options...
Barand Posted February 6 Share Posted February 6 Solution1, naturally because it is the simplest. (Although I did once have a holiday job working for a guy who would prefer solution2 as multiplication was beyond him) But being a one-liner doesn't always make it the simplest ... $solution4 = array_sum(array_map(fn($v) => $v / sin(M_PI/6), array_fill_keys(range(0,4), 1))); Quote Link to comment https://forums.phpfreaks.com/topic/317694-directory-file-list/#findComment-1614817 Share on other sites More sharing options...
Psycho Posted February 6 Share Posted February 6 1 hour ago, rick645 said: For every problem, we always tend to look for the simplest solution $solution1 = 2 * 5; $solution2 = 2 + 2 + 2 + 2 + 2; for ($i = 1; $i <= 5; $i++) { $solution3 = $i * 2 . PHP_EOL; } Which do you prefer? That's not a valid comparison. There is logic within the PHP engine to know what to do with the asterisk symbol. It was purpose built to do multiplication. If it did not exist and you wanted to be able to multiply arbitrary numbers you would have to build a function. For example, if the PHP engine knew how to add, but not to multiple here would be one way: function product ($a, $b) { $product = 0; for ($i=0; $i<$a; $i++) { $product += $b; } return $product; } Using the asterisk is kind of like calling an internal function to do the multiplication. For your purposes, you can create a function to do what you need and call that function as a single line. outputDirectory($rootDirectroy); Your logic of a single line is the simplest solution is predicated on the assumption that there is a purpose built function for your needs. If not, one needs to be created. And, I will take readable, multi-line code over condensed code every time. It's a pain to go back to some old code that needs to be refactored and trying to decipher some complicated process that was condensed down to a few lines trying to figure out how it works. Give me separate lines for each step in the process with comments on what it is expected to do. Quote Link to comment https://forums.phpfreaks.com/topic/317694-directory-file-list/#findComment-1614819 Share on other sites More sharing options...
rick645 Posted February 7 Author Share Posted February 7 On 5/2/2024 at 21:22, requinix said: Le battute sono fantastiche e tutto il resto, ma vale tutto il tempo che ci hai dedicato finora? Un semplice ciclo foreach avrebbe potuto risolvere il problema ore fa. since php doesn't offer anything native, I'd say this is the solution Quote Link to comment https://forums.phpfreaks.com/topic/317694-directory-file-list/#findComment-1614868 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.