排序在编程中是一个重要的东西,php7对数组的排序简化了不少,这是我自己测试过的,大家可以看看,由于有不少于一个实例,所以你们在用我的东西进行排序时,要先注释掉其余的部分,代码如下:
<?php
header("Content-type:text/html;charset=utf-8");
/**
此文件为测试php7新特性
*/
//对数组进行排序
/**
$arr = array(
array('fee'=>9680,'course'=>'struct'),
array('fee'=>5680,'course'=>'php'),
array('fee'=>1680,'course'=>'javascript'),
array('fee'=>8680,'course'=>'c++'),
array('fee'=>6680,'course'=>'java'),
array('fee'=>3680,'course'=>'python'),
);
function user_sort($a,$b){
if($a['fee']>$b['fee']){
return true;
}elseif($a['fee']<$b['fee']){
return false;
}else{
return 0;
}
}
usort($arr,'user_sort');
var_dump($arr);//按fee排序后的结果
**/
$db = mysqli_connect('localhost','root','','products');
$sql = 'select id,parent_id,product_name,addtime,updatetime from products where p_status=1 order by addtime';
$result = mysqli_query($db,$sql);
while(list($id,$parent_id,$product_name,$addtime,$updatetime)= mysqli_fetch_array($result,MYSQLI_NUM)){
$products[]=array('id'=>$id,'parent_id'=>$parent_id,'product_name'=>$product_name,'addtime'=>$addtime,'updatetime'=>$updatetime);
//任意组装数组,比如四维数组
};
/**
function product_sort($a,$b){
return strcmp($a['product_name'],$b['product_name']);//根据名称排序
}
**/
//根据添加时间排序
/**
function addtime_sort($a,$b){
if($a['addtime']>$b['addtime']){
return false;
}elseif($a['addtime']<$b['addtime']){
return true;
}else{
return 0;
}
}
**/
//根据修改时间排序
/**
function updatet_sort($a,$b){
if($a['updatetime']>$b['updatetime']){
return false;
}elseif($a['updatetime']<$b['updatetime']){
return true;
}else{
return 0;
}
}
**/
//根据父类id排序
/**
function parentid_sort($a,$b){
if($a['parent_id']>$b['parent_id']){
return false;
}elseif($a['parent_id']<$b['parent_id']){
return true;
}else{
return 0;
}
}
**/
//usort($products,'addtime_sort');//根据添加时间降序排序
//usort($products,'updatet_sort');//根据修改时间降序排序
//usort($products,'parentid_sort');//根据父类id降序排序
?>
以下是trait的使用方式
//测试trait
/**
trait tSomeTrait{
function userRegister(){
echo '测试trait';
}
}
class Register{
use tSomeTrait;
function uppasswd(){
return true;
}
}
$obj = new Register();
$obj->userRegister();//输出测试trait
**/
trait tDebug{
public function dumpObject(){
$class = get_class($this);
$attributes = get_object_vars($this);
$methods = get_class_methods($this);
echo '类的名字是:'.$class.'<br/>';
echo '属性有:<br/>';
foreach($attributes as $k=>$v){
echo $k.'=>'.$v.'<br/>';
}
echo '方法有:<br/>';
foreach($methods as $key=>$val){
echo $val.'<br/>';
}
}
}
class UserInfo{
use tDebug;
public $width=0;
public $height=0;
function __construct($w=0,$h=0){
$this->width=$w;
$this->height=$h;
}
public function setSize($w=0,$h=0){
$this->width=$w;
$this->height=$h;
}
function getArea(){
return ($this->width*$this->height);
}
function getperimeter(){
return (($this->width+$this->height)*2);
}
private function isSqure(){
if($this->width==$this->height){
return true;
}else{
return false;
}
}
}
$obj = new UserInfo(3,5);
$obj->dumpObject();//trait的方法与类中方法同名,类中的方法优先级高于trait中的方法,若该类继承了另一个类,则trait中的方法高于父类方法。
//trait中可以有抽象方法,trait:主要是复用代码。