01.查找当前日期和时间
$time = date('Y-m-d H:i:s');
echo $time;
var_dump(date('r'));
getdate — 取得日期/时间信息
$today = getdate();
print_r($today);
Array
(
[seconds] => 21
[minutes] => 9
[hours] => 10
[mday] => 29
[wday] => 4
[mon] => 7
[year] => 2021
[yday] => 209
[weekday] => Thursday
[month] => July
[0] => 1627524561
)
localtime - 函数返回本地时间(一个数组)。
$today = localtime(time(),true);
print_r($today);
Array
(
[tm_sec] => 27
[tm_min] => 12
[tm_hour] => 10
[tm_mday] => 29
[tm_mon] => 6
[tm_year] => 121
[tm_wday] => 4
[tm_yday] => 209
[tm_isdst] => 0
)
02.从字符串解析日期和时间
strtotime — 将任何字符串的日期时间描述解析为 Unix 时间戳
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
echo date('Y-m-d H:i:s',strtotime('+1 month'));
数组
01.指定并非从0开始的数组
$arr = [ 1 => 'stark','nike','min'];
print_r($arr);
Array
(
[1] => stark
[2] => nike
[3] => min
)
$arr = [ -1 => 'stark','nike','min'];
print_r($arr);
02.数组初始化为整数范围
range — 根据范围创建数组,包含指定的元素
$numberArr = range(1,10);
var_dump($numberArr );
$odd = range(1,10,2);
$even = $odd = range(2,10,2);
03.迭代处理数组
foreach($arr as $value){
}
foreach($arr as $key => $value){
}
$arr = [ 'stark','nike','min'];
for( $i=0; $i<= count($arr) - 1;$i++ ){
echo $arr[$i].$i.PHP_EOL;
}
while(list($key,$val) = each($arr)){
var_dump($key,$val);
}
04.从数组删除元素
unset — 释放给定的变量
unset ($foo);
unset ($bar['quux']);
unset($foo1, $foo2, $foo3);
array_splice — 去掉数组中的某一部分并用其它值取代
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);
var_dump($input);
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, -1);
var_dump($input);
$input = array("red", "green", "blue", "yellow");
array_splice($input, 1, count($input), "orange");
var_dump($input);
$input = array("red", "green", "blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));
var_dump($input);
05.改变数组的大小
array_pad — 以指定长度将一个值填充进数组
$input = array(12, 10, 9);
$result = array_pad($input, 5, 0);
$result = array_pad($input, -7, -1);
$result = array_pad($input, 2, "noop");
$result = array_pad($input, 5, "noop");
array_splice — 去掉数组中的某一部分并用其它值取代
$input = array(12, 10, 9);
$result = array_splice($input,2);
var_dump($result,$input);
06.将数组追加到另外一个数组
array_merge — 合并一个或多个数组
a
r
r
a
y
1
+
array1+
array1+array2 和 array_merge的不同点在于 前者会覆盖,保留第一个数组里的键值,array_merge会保留所有键值,重新索引
$array1 = array(0 => 'zero_a', 7 => 'one_b', 3 => 'three_a');
$array2 = array(1 => 'one_b', 3 => 'three_b', 4 => 'four_b');
$result = $array1 + $array2;
var_dump($result);
var_dump(array_merge($array1,$array2));
如果array_merge是索引数组,也会覆盖,留下后面的值(亲测)
$a = array('a','b'=>'b');
$b = array('A','b'=>'B');
$result = array_merge($a ,$b);
$c = $a + $b;
var_dump( $result, $c );
07.将数组转化为字符串
implode — 将一个一维数组的值转化为字符串,又名join
$array = array('lastname', 'email', 'phone');
$comma_separated = implode(",", $array);
08.检查一个键是不是在数组中
array_key_exists — 检查数组里是否有指定的键名或索引
$search_array = array('first' => null, 'second' => 4);
isset($search_array['first']);
var_dump($search_array);
$bool = array_key_exists('first', $search_array);
var_dump($bool);
09.检查一个元素是否在数组中
array_key_exists — 检查数组里是否有指定的键名或索引
$search_array = array('first' => null, 'second' => 4);
isset($search_array['first']);
var_dump($search_array);
$bool = array_key_exists('first', $search_array);
var_dump($bool);
10.最大值和最小值
max — 找出最大值
<?php
echo max(1, 3, 5, 6, 7);
echo max(array(2, 4, 5));
echo max(0, 'hello');
echo max('hello', 0);
echo max('42', 3);
echo max(-1, 'hello');
$val = max(array(2, 2, 2), array(1, 1, 1, 1));
$val = max(array(2, 4, 8), array(2, 5, 7));
$val = max('string', array(2, 5, 7), 42);
?>
min — 找出最小值
<?php
echo min(2, 3, 1, 6, 7);
echo min(array(2, 4, 5));
echo min(0, 'hello');
echo min('hello', 0);
echo min('hello', -1);
$val = min(array(2, 4, 8), array(2, 5, 1));
$val = min('string', array(2, 5, 7), 42);
11.删除重复的值
array_unique — 移除数组中重复的值
注意键名保留不变。array_unique() 先将值作为字符串排序,然后对每个值只保留第一个遇到的键名,接着忽略所有后面的键名。这并不意味着在未排序的 array 中同一个值的第一个出现的键名会被保留。
$input = array(4, "4", "3", 4, 3, "3");
$result = array_unique($input);
var_dump($result);
|