array_filter - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of array_filter.php

  1. <?php
  2.  
  3. const ALLOWED_EXTENSIONS = ['gif', 'jpg', 'jpeg'];
  4.  
  5. $files = [
  6.     '1047_0.gif',
  7.     '1047_0.txt',
  8.     '1047_0.pdf',
  9.     '1047_0.jpeg',
  10.     '1047_0.jpg',
  11. ];
  12.  
  13. $filesToKeep = array_filter(
  14.     $files,
  15.     function($file) {
  16.         $extension = explode('.', $file)[1];
  17.         
  18.         return \in_array($extension, ALLOWED_EXTENSIONS, true);
  19.     }
  20. );
  21.  
  22. var_dump($filesToKeep);
File Description
  • array_filter
  • PHP Code
  • 27 Oct-2020
  • 379 Bytes
You can Share it: