源数组
`
$data=array(
'0'=>array(
'id'=>'1',
'sold'=>15
),
'1'=>array(
'id'=>'2',
'sold'=>10
),
'2'=>array(
'id'=>'3',
'sold'=>8
),
'3'=>array(
'id'=>'5',
'sold'=>30
),
'4'=>array(
'id'=>'7',
'sold'=>12
),
'5'=>array(
'id'=>'12',
'sold'=>15
),
'6'=>array(
'id'=>'17',
'sold'=>3
)
);
`
需求
-
functionmaopao($data,$key,$sort){
}
说明:将一个二维数组以某一个第二维的值进行升序或者降序排列,其中参数$data为一个
二维数组,$key为第二维的键名,$sort为升序或者降序排列方式。 -
functionzhenghe($data,$key){
}
说明:将数组按照某一个第二维的值进行整合,其中参数$data为一个二维数组,$key为第
二维的键名 -
functionzhuanhuan($data,$type){
}
说明:将数组进行格式转换返回值为对应格式的字符串,其中其中参数$data为一个二维数
组,$type为转换格式,其中有:json,xml,stdclass,object四种类型, -
数据导出
functiondaochu($data,$type){
}
说明:将数组进行导出,导出的文件为对应格式,其中其中参数$data为一个二维数组,$type
为转换格式,其中有:xls,xml,ini三种类型,
实现代码
`
$data[$j][$key]){
$temp=$data[$i];
$data[$i]=$data[$j];
$data[$j]=$temp;
}
}
}
foreach($data as $key=>$val){
foreach($val as $k=>$v){
echo $k.':'.$v;
}
echo '
';
}
}else{
for($i=0;$i$val){
foreach($val as $k=>$v){
echo $k.':'.$v;
}
echo '
';
}
}
}
//返回二维数组第二维键名
public static function arrayTwo($data){
$num = 0;
foreach($data as $key=>$val) {
foreach ($val as $k => $v){
if($num==1)
return $k;
$num++;
}
}
}
/**
* @param $data 多维数组
* @param $key 第二维键名
*/
function zhenghe($data,$key){
//判断键名
if($key!=HuBiao::arrayTwo($data)){
echo '键值输入错误';
return;
}
$newArray=array();
foreach($data as $temp){
$newArray[$temp[$key]][]=$temp;
}
//输出测试
foreach($newArray as $table){
foreach($table as $row){
foreach($row as $col){
echo $col.' ';
}
echo '
';
}
echo '
';
}
}
/**
* @param $data 数组
* @param $type 转换类型 'json''xml''object''std'
*/
function zhuanhuan($data,$type){
if($type=='json'){
return json_encode($data);
}else if($type=='xml'){
return HuBiao::arrayToXml($data);
}else {
/*$obj = new stdClass();
foreach($data as $key => $value){
$obj -> $key = $value;
}
return $obj;*/
return json_decode(json_encode($data));
}
}
//转换xml的中间函数
public static function arrayToXml($arr){
$xl = '';
foreach ($arr as $key=>$val){
if(is_array($val)){
$xl.=htmlspecialchars("<").$key.htmlspecialchars(">").HuBiao::arrayToXml($val).htmlspecialchars("").$key.htmlspecialchars(">");
}else{
$xl.= htmlspecialchars("<").$key.htmlspecialchars(">").$val.htmlspecialchars("").$key.htmlspecialchars(">");
}
}
return $xl;
}
/**
* @param $data 数组
* @param $type 导出文件类型 'excell'
*/
function daochu($data,$type){
if($type=='excell'){
$title = array('顺序号','商品id','售卖时间','数量','金额');
HuBiao:: exportexcel($data,$title,'用户表');
}else if($type=='xml'){
// 创建新的simpleXML实例,注入内存,确定根节点
$xml = new SimpleXMLElement(' ');
// 回调函数
HuBiao::array_to_XML($xml, $data);
// 保存为XML文件
$filename = 'C:/Users/Public/Desktop/data.xml';
echo (($xml->asXML($filename)) ? 'Your XML file has been generated successfully! Location: 桌面 data.xml':'Error generating XML file!');
}else{
$iniString = '';
foreach($data as $key=>$val){
$iniString.='{'.'item'.$key.'}'."\r\n";
foreach ($val as $ck => $cv){
$iniString.= $ck.':'.$cv."\r\n";
}
}
$filename = 'C:/Users/Public/Desktop/data.ini';
echo file_put_contents($filename,$iniString)? 'Your XML file has been generated successfully! Location:桌面 data.ini':'Error generating XML file!';
}
}
/**
2 * 导出数据为excel表格
3 *@param $data 一个二维数组,结构如同从数据库查出来的数组
4 *@param $title excel的第一行标题,一个数组,如果为空则没有标题
5 *@param $filename 下载的文件名
6 *@examlpe1*/
public static function exportexcel($data=array(),$title=array(),$filename='report'){
ob_end_clean();
ob_start();
header("Content-type:application/octet-stream");
header("Accept-Ranges:bytes");
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=".urlencode($filename).".xls");
header("Pragma: no-cache");
header("Expires: 0");
//导出xls 开始
if (!empty($title)){
foreach ($title as $k => $v) {
$title[$k]=iconv("UTF-8", "GB2312",$v);
}
$title= implode("\t", $title);
echo "$title\n"; }
if (!empty($data)){
$numt = 0;
$sumpricet = 0;
foreach($data as $key=>$val){
foreach ($val as $ck => $cv) {
if($ck=='time'){
$myDateTime = DateTime::createFromFormat('Y-m-d h:i:s', $cv);
$cv = $myDateTime->format('Y/m/d h:i');
}
if($ck=='num'){
$numt = $numt+$cv;
}
if($ck=='sumprice'){
$sumpricet = $sumpricet+$cv;
}
$data[$key][$ck]=iconv("UTF-8", "GB2312", $cv);
}
$data[$key]=implode("\t ", $data[$key]);
}
echo implode("\n",$data);
echo "\n\t\t";
$name = iconv("UTF-8", "GB2312",'总和:');
$temp = array($name,$numt,$sumpricet);
$temp= implode("\t", $temp);
echo "$temp\n";
}
}
// 到处到XMl文件的中间函数
public static function array_to_XML($obj, $array)
{
foreach ($array as $k => $v)
{
if(is_numeric($k))
$k = 'item' . $k;
if(is_array($v)){
$node = $obj->addChild($k);
HuBiao::array_to_XML($node, $v);
}else{
$obj->addChild($k, htmlspecialchars($v));
}
}
}
}
//冒泡测试
/* $data = array(
'0' => array(
'id' => '1',
'sold' => 15
),
'1' => array(
'id' => '2',
'sold' => 10
),
'2' => array(
'id' => '3',
'sold' => 8
),
'3' => array(
'id' => '5',
'sold' => 30
),
'4' => array(
'id' => '7',
'sold' => 12
),
'5' => array(
'id' => '12',
'sold' => 18
),
'6' => array(
'id' => '17',
'sold' => 3
)
);
$key = 'sold';
$sort = 'DESC';
$hb = new HuBiao();
$hb->maopao($data,$key,$sort);*/
/*//zhenghe()测试
$data = array(
'0' => array(
'id' => '1',
'goods_id' => 'g001',
'time' => '2020-03-25 09:01:34'
),
'1' => array(
'id' => '2',
'goods_id' => 'g003',
'time' => '2020-03-25 09:10:15'
),
'2' => array(
'id' => '3',
'goods_id' => 'g001',
'time' => '2020-03-25 09:35:49'
),
'3' => array(
'id' => '4',
'goods_id' => 'g004',
'time' => '2020-03-25 09:38:14'
),
'4' => array(
'id' => '5',
'goods_id' => 'g001',
'time' => '2020-03-25 10:32:34'
)
);
$hb = new HuBiao();
$hb->zhenghe($data,'goods_id');*/
//格式转换测试
/* $data = array(
'0' => array(
'id' => '1',
'goods_id' => 'g001',
'time' => '2020-03-25 09:01:34'
),
'1' => array(
'id' => '2',
'goods_id' => 'g003',
'time' => '2020-03-25 09:10:15'
),
'2' => array(
'id' => '3',
'goods_id' => 'g004',
'time' => '2020-03-25 09:35:49'
),
'3' => array(
'id' => '4',
'goods_id' => 'g001',
'time' => '2020-03-25 09:38:14'
),
'4' => array(
'id' => '5',
'goods_id' => 'g001',
'time' => '2020-03-25 10:32:34'
)
);*/
//转换为xml测试
/*$hb = new HuBiao();
$hb->zhuanhuan($data,'xml');*/
//转换为object测试
/*$hb = new HuBiao();
print_r($hb->zhuanhuan($data,'std')) ;*/
//导出Excell测试
/*$data = array(
'0' => array(
'id' => '1',
'goods_id' => 'g001',
'time' => '2020-03-25 09:01:34',
'num' => 1,
'sumprice' => 25.00
),
'1' => array(
'id' => '2',
'goods_id' => 'g003',
'time' => '2020-03-25 09:10:15',
'num' => 2,
'sumprice' => 49.00
),
'2' => array(
'id' => '3',
'goods_id' => 'g004',
'time' => '2020-03-25 09:35:49',
'num' => 1,
'sumprice' => 30.00
),
'3' => array(
'id' => '4',
'goods_id' => 'g001',
'time' => '2020-03-25 09:38:14',
'num' => 3,
'sumprice' => 75.00
),
'4' => array(
'id' => '5',
'goods_id' => 'g001',
'time' => '2020-03-25 10:32:34',
'num' => 1,
'sumprice' => 25.00
)
);
$hb = new HuBiao();*/
//$hb->daochu($data,'excell');
//$hb->daochu($data,'xml');
//$hb->daochu($data,'ini');
`