15个PHP常用函数
1.统计数组元素个数
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
$arr = array( '1011,1003,1008,1001,1000,1004,1012', '1009', '1011,1003,1111' ); $result = array(); foreach ($arr as $str) { $str_arr = explode(',', $str); foreach ($str_arr as $v) { // $result[$v] = isset($result[$v]) ? $result[$v] : 0; // $result[$v] = $result[$v] + 1; $result[$v] = isset($result[$v]) ? $result[$v]+1 : 1; } } print_r($result); //Array ( [1011] => 2 [1003] => 2 [1008] => 1 [1001] => 1 [1000] => 1 [1004] => 1 [1012] => 1 [1009] => 1 [1111] => 1 ) |
2. 循环删除目录
0 1 2 3 4 5 6 7 8 9 10 11 |
function cleanup_directory($dir) { foreach (new DirectoryIterator($dir) as $file) { if ($file->isDir()) { if (! $file->isDot()) { cleanup_directory($file->getPathname()); } } else { unlink($file->getPathname()); } } rmdir($dir); } |