個人技術作品 - 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開發中所經常遇到的問題。 
發佈了40 篇原創文章 · 獲贊 1 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章