1.php二维数组,根据指定键排序函数
/**
* 二维数组排序
* @param type $arr 数组
* @param type $keys 键
* @param type $type 排序方式:asc|desc
* @return type
*/
function arraySort($arr, $keys, $type = ''asc'') {
$keysvalue = $new_array = array();
foreach ($arr as $k => $v){
$keysvalue[$k] = $v[$keys];
}
$type == ''asc'' ? asort($keysvalue) : arsort($keysvalue);
reset($keysvalue);
foreach ($keysvalue as $k => $v) {
$new_array[$k] = $arr[$k];
}
return $new_array;
}
方法2: php5.5以上版本
function arraySequence($array, $field, $sort = 'SORT_DESC')
{
$arrSort = array();
foreach ($array as $uniqid => $row) {
foreach ($row as $key => $value) {
$arrSort[$key][$uniqid] = $value;
}
}
array_multisort($arrSort[$field], constant($sort), $array);
return $array;
}
2.PHP函数array_column() 用于返回输入数组中某一列的值
/**
* 兼容低于PHP 5.5版本的array_column()函数
*/
if(!function_exists('array_column')){
function array_column($input, $columnKey, $indexKey = NULL){
$columnKeyIsNumber = (is_numeric($columnKey)) ? TRUE : FALSE;
$indexKeyIsNull = (is_null($indexKey)) ? TRUE : FALSE;
$indexKeyIsNumber = (is_numeric($indexKey)) ? TRUE : FALSE;
$result = array();
foreach ((array)$input AS $key => $row)
{
if ($columnKeyIsNumber)
{
$tmp = array_slice($row, $columnKey, 1);
$tmp = (is_array($tmp) && !empty($tmp)) ? current($tmp) : NULL;
}
else
{
$tmp = isset($row[$columnKey]) ? $row[$columnKey] : NULL;
}
if ( ! $indexKeyIsNull)
{
if ($indexKeyIsNumber)
{
$key = array_slice($row, $indexKey, 1);
$key = (is_array($key) && ! empty($key)) ? current($key) : NULL;
$key = is_null($key) ? 0 : $key;
}
else
{
$key = isset($row[$indexKey]) ? $row[$indexKey] : 0;
}
}
$result[$key] = $tmp;
}
return $result;
}
}
3.PHP格式化时间
/**
* 格式化显示时间
* @param $dateStr 时间戳
*/
function format_date($dateStr, $format = '') {
$limit = time() - $dateStr;
$r = "";
if ($limit < 60) {
$r = '刚刚';
} elseif ($limit >= 60 && $limit < 3600) {
$r = floor($limit / 60) . '分钟前';
} elseif ($limit >= 3600 && $limit < 86400) {
$r = floor($limit / 3600) . '小时前';
} elseif ($limit >= 86400 && $limit < 2592000) {
$day = floor($limit / 86400);
if($day < 8){
$r = floor($limit / 86400) . '天前';
}else{
$r = date($format ? $format : 'Y-m-d H:i', $dateStr);
}
}
// elseif ($limit >= 2592000 && $limit < 31104000) {
// $r = floor($limit / 2592000) . '个月前';
// }
else {
$r = date($format ? $format : 'Y-m-d H:i', $dateStr);
}
return $r;
}
4.PHP自定义函数array_key_chunk实现根据指定Key分组
array_chunk(array,size,preserve_key) 函数是把数组分割为新的数组块。 其中每个数组的单元数目由 size 参数决定。最后一个数组的单元数目可能会少几个。 可选参数 preserve_key 是一个布尔值,它指定新数组的元素是否有和原数组相同的键(用于关联数组),还是从 0 开始的新数字键(用于索引数组)。默认是分配新的键。
/**
* array_key_chunk
* 自定义函数:
* @param Array KEY Field
*/
function array_key_chunk($array, $key, $field = array()){
$items = array();
foreach($array as $item) {
$key_id = $item[$key];
unset($item[$key]);
unset($item['setting']);
if(!isset($items[$key_id])) {
$items[$key_id] = array($key=>$key_id, 'items'=>array());
}
if(is_array($field) && !empty($field)){
$new_item = array();
foreach($field as $val){
$new_item[$val] = $item[$val];
}
$items[$key_id]['items'][] = $new_item;
}else{
$items[$key_id]['items'][] = $item;
}
}
return $items;
}
5.PHP序列化中文问题
/*
* 解决将gb2312转换成utf-8格式之后,
* 每个中文的字节数从2个增加到3个之后导致了反序列化的时候判断字符长度出现了问题
*/
function mb_unserialize($serial_str) {
$out = preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'", $serial_str );
return unserialize($out);
}
6.PHP字符串&数组间转换
/**
* 将字符串转换为数组
*
* @param string $data 字符串
* @return array 返回数组格式,如果,data为空,则返回空数组
*/
function string2array($data) {
if ($data == '')
return array();
@eval("\$array = $data;");
return $array;
}
/**
* 将数组转换为字符串
*
* @param array $data 数组
* @param bool $isformdata 如果为0,则不使用new_stripslashes处理,可选参数,默认为1
* @return string 返回字符串,如果,data为空,则返回空
*/
function array2string($data, $isformdata = 1) {
if ($data == '')
return '';
if ($isformdata)
$data = new_stripslashes($data);
return addslashes(var_export($data, TRUE));
}
7.PHP - GD合成图片、生成文字、居中对齐、画线、矩形、三角形、多边形、图片抗锯齿、不失真
function generateImg($source, $text1, $text2, $text3, $font = './msyhbd.ttf') {
$date = '' . date ( 'Ymd' ) . '/';
$img = $date . md5 ( $source . $text1 . $text2 . $text3 ) . '.jpg';
if (file_exists ( './' . $img )) {
return $img;
}
$main = imagecreatefromjpeg ( $source );
$width = imagesx ( $main );
$height = imagesy ( $main );
$target = imagecreatetruecolor ( $width, $height );
$white = imagecolorallocate ( $target, 255, 255, 255 );
imagefill ( $target, 0, 0, $white );
imagecopyresampled ( $target, $main, 0, 0, 0, 0, $width, $height, $width, $height );
$fontSize = 18;//像素字体
$fontColor = imagecolorallocate ( $target, 255, 0, 0 );//字的RGB颜色
$fontBox = imagettfbbox($fontSize, 0, $font, $text1);//文字水平居中实质
imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 190, $fontColor, $font, $text1 );
$fontBox = imagettfbbox($fontSize, 0, $font, $text2);
imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 370, $fontColor, $font, $text2 );
$fontBox = imagettfbbox($fontSize, 0, $font, $text3);
imagettftext ( $target, $fontSize, 0, ceil(($width - $fontBox[2]) / 2), 560, $fontColor, $font, $text3 );
//imageantialias($target, true);//抗锯齿,有些PHP版本有问题,谨慎使用
imagefilledpolygon ( $target, array (10 + 0, 0 + 142, 0, 12 + 142, 20 + 0, 12 + 142), 3, $fontColor );//画三角形
imageline($target, 100, 200, 20, 142, $fontColor);//画线
imagefilledrectangle ( $target, 50, 100, 250, 150, $fontColor );//画矩形
//bof of 合成图片
$child1 = imagecreatefromjpeg ( 'http://baboben.com/i1/T1N0pxFEhaXXXxK1nM-357-88.jpg' );
imagecopymerge ( $target, $child1, 0, 400, 0, 0, imagesx ( $child1 ), imagesy ( $child1 ), 100 );
//eof of 合成图片
@mkdir ( './' . $date );
imagejpeg ( $target, './' . $img, 95 );
imagedestroy ( $main );
imagedestroy ( $target );
imagedestroy ( $child1 );
return $img;
}
//http://baboben.com
generateImg ( 'http://ig.baboben.com/munv/pic.jpg', 'baboben.com', 'PHP文字水平居中', '3个字' );
exit ();
8. PHP时间转换 - 格式化日期/时间戳 /**
- PHP时间转换
- 刚刚、几分钟前、几小时前、今天昨天前天几天前
- @param string $targetTime 时间戳
- @return string
- /
function get_last_time($targetTime)
{
// 今天最大时间
$todayLast = strtotime(date('Y-m-d 23:59:59'));
$agoTimeTrue = time() - $targetTime;
$agoTime = $todayLast - $targetTime;
$agoDay = floor($agoTime / 86400);
if ($agoTimeTrue < 60) {
$result = '刚刚';
} elseif ($agoTimeTrue < 3600) {
$result = (ceil($agoTimeTrue / 60)) . '分钟前';
} elseif ($agoTimeTrue < 3600 * 12) {
$result = (ceil($agoTimeTrue / 3600)) . '小时前';
} elseif ($agoDay == 0) {
$result = '今天 ' . date('H:i', $targetTime);
} elseif ($agoDay == 1) {
$result = '昨天 ' . date('H:i', $targetTime);
} elseif ($agoDay == 2) {
$result = '前天 ' . date('H:i', $targetTime);
} elseif ($agoDay > 2 && $agoDay < 16) {
$result = $agoDay . '天前 ' . date('H:i', $targetTime);
} else {
$format = date('Y') != date('Y', $targetTime) ? "Y-m-d H:i" : "m-d H:i";
$result = date($format, $targetTime);
}
return $result;
}