个人技术作品 - PHP Array 常用排序及数据高级操作的 API Engine

<?php
/**
 * fun.ArrayApi.php
 * 
 * GMT 系列 - 功能库接口 1
 * 
 * 功能库名: 数组操作系列
 * 
 * 功能库内容介绍:
 *         1. 格林威治时间快速转时间戳
 *         2. usort 自动排序二维数组方法 1
 *             1). 对大写字母或数字进行升序排列
 *             2). 对大写字母或数字进行降序排列
 *         3. usort 自动排序二维数组方法 2
 *             1). 对时间进行升/降序排列
 *         4. 把数组中带有时间戳的字段,实时修改为格林威治格式
 *         5. 把数组中带有格林威治时间的字段,实时修改为时间戳
 * 
 *  global $index 为功能 2,3 的公共变量 , 由 $index 来控制 2,3 功能所要操作数组的字段
 * 
 *     @example 
 *         1. int void GmtToUnix(datetime) 
 *             返回时间戳 , datetime 可自由适配,只支持以 / ,- 为分割年月日的切割符,年月日时分秒可随意缺少增加
 * 
 *         2. void function ORD_ASC / ORD_DESC
 *             配合 global $index 及 php 自带函数 usort() 使用,usort()查看php手册
 * 
 *             @example:
 *             $exar = array(0=>array('a','1','A'),1=>array('b','2','B'));
 *             $index = 1;
 *             usort($exar,"ORD_ASC");  || usort($exar,"ORD_DESC");
 *             
 *         3. void function DATE_ASC / DATE_DESC
 *             配合 global $index 及 php 自带函数 usort() 使用,usort()查看php手册
 * 
 *             @example:
 *             $exar = array(0=>array('2005-08-21 13:21:77','A'),1=>array('2007-12-11 11:21:77','B'));
 *             $index = 0;
 *             usort($exar,"DATE_ASC");  || usort($exar,"DATE_DESC");
 * 
 *         4. void array ARY_UTG($index,$arr,$Type = true,$format_string)
 *             $index 数组字段索引
 *             $arr 源数组
 *             $type 数组级别 true 为二维数组(默认),false为一维数组
 *             $format_string 格林威治时间样式 默认为 "Y-m-d H:i:s"
 *             
 *             @example:
 *             $exar = array(array(1176731096,'c1932'),array(1076731096,'i1932'));
 *             $exar = ARY_UTG(0,$exar);
 *             print_r($exar);
 * 
 *         5. void array ARY_GTU($index,$arr,$Type = true)
 *             $index 数组字段索引
 *             $arr 源数组
 *             $type 数组级别 true 为二维数组(默认),false为一维数组
 *             
 *             @example:
 *             $exar = array(array('2003-03-28 22:31:21','f1932'),array('2005-07-18 22:31:21','i1932'));
 *             $exar = ARY_UTG(0,$exar);
 *             print_r($exar);
 
*/


global $Index;

/**
 * 把格林威治时间转为时间戳
 
*/
function GmtToUnix($GmtDate)
{
    
$DateArr = explode(' ',$GmtDate); // 分割GMT日期为 日期 时间

    
/* 在日期中取得年,月,日 */
    
$pDate = split('[/.-]',$DateArr[0]);
    
$Year = $pDate[0];
    
$Month = $pDate[1];
    
$Day = $pDate[2];
    
    
/* 在时间中取得时,分,秒 */
    
$pTime = split('[:.-]',$DateArr[1]);
    
$Hour = $pTime[0];
    
$Minute = $pTime[1];
    
$Second = $pTime[2];
    
    
if($Year == '' || !is_numeric($Year))$Year = 0;
    
if($Month == '' || !is_numeric($Month))$Month = 0;
    
if($Day == '' || !is_numeric($Day))$Day = 0;
    
if($Hour == '' || !is_numeric($Hour))$Hour = 0;
    
if($Minute == '' || !is_numeric($Minute))$Minute = 0;
    
if($Second == '' || !is_numeric($Second))$Second = 0;
    
    
return mktime($Hour,$Minute,$Second,$Month,$Day,$Year);
}

/**
 * 对大小写字母和数字进行升序排列
 
*/
function ORD_ASC($ax,$ay)
{
    
global $Index;
    
if($ax[$Index== $ay[$Index]) {
        
return 0;
    } 
elseif($ax[$Index< $ay[$Index]) {
        
return -1;
    } 
else {
        
return 1;
    }
}

/**
 * 对大小写字母和数字进行降序排列
 
*/
function ORD_DESC($ax,$ay)
{
    
global $Index;
    
if($ax[$Index== $ay[$Index]) {
        
return 0;
    } 
elseif($ax[$Index> $ay[$Index]) {
        
return -1;
    } 
else {
        
return 1;
    }
}

/**
 * 对日期字符进行升序排列
 
*/
function DATE_ASC($ax,$ay)
{
    
global $Index;
    
if(GmtToUnix($ax[$Index]) == GmtToUnix($ay[$Index])) {
        
return 0;
    } 
elseif(GmtToUnix($ax[$Index]) < GmtToUnix($ay[$Index])) {
        
return -1;
    } 
else {
        
return 1;
    }
}

/**
 * 对日期字符进行降序排列
 
*/
function DATE_DESC($ax,$ay)
{
    
global $Index;
    
if(GmtToUnix($ax[$Index]) == GmtToUnix($ay[$Index])) {
        
return 0;
    } 
elseif(GmtToUnix($ax[$Index]) > GmtToUnix($ay[$Index])) {
        
return -1;
    } 
else {
        
return 1;
    }
}

/**
 * 对数据集数组的时间戳转换为格林威治时间
 * $index 需要修改数据的数组字段
 * $Type 数组类型,一维为true 二维为false, 最高支持二维,默认二维
 * $arr 源数组
 * $format_string 将时间戳格式为以为 $format_string 为例的时间样式
 * array(array('a1932',1176731096,'c1932'))
 
*/
function ARY_UTG($index = 0, $arr, $Type = true, $format_string = "Y-m-d H:i:s")
{
    
reset($arr);
    
if($Type){
        
foreach ($arr as $k => $value) {
            
$arr[$k][$index= @gmdate($format_string,$arr[$k][$index]);
        }
    } 
else {
        
foreach ($arr as $k => $value) {
            
$arr[$index= @gmdate($format_string,$arr[$index]);
        }
    }
    
return $arr;
}

/**
 * 对数据集数组的格林威治时间转换为时间戳
 * $index 需要修改数据的数组字段
 * $Type 数组类型,一维为true 二维为false, 最高支持二维,默认二维
 * $arr 源数组
 * array(array('a1932',1176731096,'c1932'))
 
*/
function ARY_GTU($index = 0, $arr, $Type = true)
{
    
reset($arr);
    
if($Type){
        
foreach ($arr as $k => $value) {
            
$arr[$k][$index= GmtToUnix($arr[$k][$index]);
        }
    } 
else {
        
foreach ($arr as $k => $value) {
            
$arr[$index= GmtToUnix($arr[$index]);
        }
    }
    
return $arr;
}
?>


具体使用方法,在类内已经有说明,请根据类内介绍进行使用。

此API为了读者能更有效的操作复杂数据及排序有着重要作用。笔者在研究PHP开发中所经常遇到的问题。 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值