php基本函数汇总
本文给大家汇总了16个常见的php基本函数,涵盖的面很广,这里推荐给大家,希望对大家学习php能够有所帮助。
1.统计数组元素个数
$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. 循环删除目录
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); }
3.无限极分类生成树
function generateTree($items){
$tree = array();
foreach($items as $item){
if(isset($items[$item['pid']])){
$items[$item['pid']]['son'][] = &$items[$item['id']];
}else{
$tree[] = &$items[$item['id']];
}
}
return $tree;
}
function generateTree2($items){
foreach($items as $item)
$items[$item['pid']]['son'][$item['id']] = &$items[$item['id']];
return isset($items[0]['son']) ? $items[0]['son'] : array();
}
$items = array(
1 => array('id' => 1, 'pid' => 0, 'name' => '安徽省'),
2 => array('id' => 2, 'pid' => 0, 'name' => '浙江省'),
3 => array('id' => 3, 'pid' => 1, 'name' => '合肥市'),
4 => array('id' => 4, 'pid' => 3, 'name' => '长丰县'),
5 => array('id' => 5, 'pid' => 1, 'name' => '安庆市'),
);
print_r(generateTree($items));
/**
* 如何取数据格式化的树形数据
*/
$tree = generateTree($items);
function getTreeData($tree){
foreach($tree as $t){
echo $t['name'].'
';
if(isset($t['son'])){
getTreeData($t['son']);
}
}
}
4.数组排序 a - b 是数字数组写法 遇到字符串的时候就要
var test = ['ab', 'ac', 'bd', 'bc']; test.sort(function(a, b) { if(a b) { return 1; } return 0; });
5.array_reduce
$raw = [1,2,3,4,5,]; // array_reduce 的第三个参数是 $result 的初始值 array_reduce($raw, function($result, $value) { $result[$value] = $value; return $result; }, []); // [1 => 1, 2 => 2, ... 5 => 5]
6.array_map 闭包中只接受一个或者多个参数,闭包的参数数量和 array_map 本身的参数数量必须一致
$input = ['key' => 'value']; array_map(function($key, $value) { echo $key . $value; }, array_keys($input), $input) // 'keyvalue' $double = function($item) { return 2 * $item; } $result = array_map($double, [1,2,3]); // 2 4 6
7.繁殖兔子
$month = 12; $fab = array(); $fab[0] = 1; $fab[1] = 1; for ($i = 2; $i "; }
8 .datetime
function getCurMonthFirstDay($date) { return date('Y-m-01', strtotime($date)); } getCurMonthLastDay('2015-07-23') function getCurMonthLastDay($date) { return date('Y-m-d', strtotime(date('Y-m-01', strtotime($date)) . ' +1 month -1 day')); }
9.加密解密
function encrypt($data, $key) { $key = md5($key); $x = 0; $len = strlen($data); $l = strlen($key); $char = ''; for ($i = 0; $i
10 . 多维数组降级
function array_flatten($arr) { $result = []; array_walk_recursive($arr, function($value) use (&$result) { $result[] = $value; }); return $result; } print_r(array_flatten([1,[2,3],[4,5]]));// [1,[2,3],[4,5]] => [1,2,3,4,5] // var new_array = old_array.concat(value1[, value2[, ...[, valueN]]]) var test = [1,2,3,[4,5,6],[7,8]]; [].concat.apply([], test); // [1,2,3,4,5,6,7,8] 对于 test 数组中的每一个 value, 将它 concat 到空数组 [] 中去,,而因为 concat 是 Array 的 prototype,所以我们用一个空 array 作载体 var test1 = [1,2,[3,[4,[5]]]]; function flatten(arr) { return arr.reduce(function(pre, cur) { if(Array.isArray(cur)) { return flatten(pre.concat(cur)); } return pre.concat(cur); }, []); } // [1,2,3,4,5] json_encode中文 function json_encode_wrapper ($result) { if(defined('JSON_UNESCAPED_UNICODE')){ return json_encode($result,JSON_UNESCAPED_UNICODE|JSON_NUMERIC_CHECK); }else { return preg_replace( array("#\\\u([0-9a-f][0-9a-f][0-9a-f][0-9a-f])#ie", "/\"(\d+)\"http://www.jb51.net/",), array("iconv('UCS-2', 'UTF-8', pack('H4', '\\1'))", "\\1"), json_encode($result) ); } }
12.二维数组去重
下一篇: 数据库事务的隔离级别简单总结