php数组比较-usort

usort()、getdate()

自定义排序,书写一个函数,对数组里面的元素大小两两进行比较,如果return -1,则将大的放在前面,如果return 1,则将大的放在后面。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php 
$arr5 = array(
array('name'=>'jack','age'=>24,'sex'=>'man'),
array('name'=>'rose','age'=>38,'sex'=>'wom'),
array('name'=>'lucy','age'=>16,'sex'=>'girl'),
array('name'=>'lili','age'=>19,'sex'=>'girl'),
array('name'=>'Tomi','age'=>27,'sex'=>'boy')
);

usort($arr5,function($num1,$num2){
if ($num1['age']==$num2['age']) {
return 0;
}
return ($num1['age']>$num2['age'] ? 1 : -1);
});

echo "<pre>";
var_dump($arr5);
echo "</pre>";

?>

将二维数组按年龄进行排序

计算上月最后一天星期几

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//方法一:玩转一周7天
$t = time();
$today = date('d',$t);//28
$weekday = localtime(time(),true);//时间数组
$week = $weekday['tm_wday'];//今天星期几
$res = ($today + $week)%7; //本月已过的天数+星期取7的余数就是上月最后一天
echo '上月最后一天星期:'.$res;

echo "<hr>";

//方法二:玩转时间戳
$today = getdate(time())['mday'];
$today2 = $today*24*60*60*1000;
$today1 = time();
$diff = $today1 - $today2;
$res2 = getdate($diff);
echo '上月最后一天星期:'.$res2['wday'];

补充小知识:

1
2
getdate()[0] == time();
getdate(time())['mday'] == date('d',time());

获取当前页面编码:mb_internal_encoding()
修改当前页面编码:mb_interval_encoding(utf-8)