0

I am using PHP 7.4.1.

I am trying to get files from a certain folder and I use the following code for this:

array_diff(scandir($views . "/Text"), array('.', '..'));

I have the following folder structure:

Texts/
├── test1.blade.php
└── Parts

test1.blade.php is a file and Parts is a folder.

The above code gives me back files AND folders.

I would only like to get all .blade.php files in my array.

Any suggestions how to filter out the folder(s)?

Appreciate your replies!

Carol.Kar
  • 4,581
  • 36
  • 131
  • 264

1 Answers1

3
  • $result = array_filter($result, 'is_file'); - files
  • $result = array_filter($result, 'is_dir'); - folders
WinterSilence
  • 413
  • 3
  • 15
  • Thx for your reply! How can I use your `array_filter` function with my `array_diff(scandir($views . "/Text"), array('.', '..'));` when passing this as an array I get an error: `array_filter('is_file',array_diff(scandir($views . "/Text"), array('.', '..')));` – Carol.Kar Nov 22 '20 at 10:29
  • ups.. my fail, switch arguments: `array_filter($result, 'is_file')` – WinterSilence Nov 22 '20 at 12:01
  • also u can use array_diff() but array_filter() remove root links in any case – WinterSilence Nov 22 '20 at 12:05