Use array_map to simply delete files and directories in PHP

  • 2021-07-22 09:26:05
  • OfStack

Don't talk nonsense, just paste the code. What this article embodies is conciseness


<?php
 
// Delete all empty directories under the directory
array_map('rmdir', glob('*', GLOB_ONLYDIR));
 
// Delete all files in the directory
array_map('unlink', array_filter(glob('*'), 'is_file'));

Implement array_column functionality using array_map:


$data = array(
    array(
        'a' => 'first a',
        'b' => 'first b'
    ),
    array(
        'a' => 'second a',
        'b' => 'second b'
    )
);
 
$array_column = array_map(function($element){
    return $element['a'];
}, $data);
 
print_r($array_column);


Related articles: