--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
--------------------------------------------------------------------------
数组对象$data->aa不存在时//tp3适用
$a = @$data->aa; // $a = '';
--------------------------------------------------------------------------
对象//tp3适用
$a = (object)array();
--------------------------------------------------------------------------
时间戳格式
strtotime("2017-11-21 11:30:30") = 1511235030
strtotime("now") = time()
date("Y-m-d H:i:s",$time);
--------------------------------------------------------------------------
判断并赋值
(flag)? true:false
例如:$res = ($a == 1)? true:false 若$a = 1,则 $res = true;否则 $res = false
in_array($a,$arr)使用
$word = (in_array($a,$arr)) ? 0:1
--------------------------------------------------------------------------
字符串截取
$a = 'afsd123jkuop';
strstr( string , like)
substr(string, start ,length)/substr( string ,start)
strstr($a,'123') = '123jkuop';
substr($a,1) = 'fsd123jkuop';
substr($a,3,5) = 'd123j';
--------------------------------------------------------------------------
字符串替换
$a = "hello world";
echo str_replace('world','china',$a) ; // echo ‘hello china’
--------------------------------------------------------------------------
正斜杠替代反斜杠
a = 'a/b\c\d';
$path=str_replace('\\','/',$a);//输出a/b/c/d
--------------------------------------------------------------------------
获取多维数组的某一列 array_column()
例子:
$user = array(
'0' => array('id' => 100, 'username' => 'a1'),
'1' => array('id' => 101, 'username' => 'a2'),
'2' => array('id' => 102, 'username' => 'a3'),
'3' => array('id' => 103, 'username' => 'a4'),
'4' => array('id' => 104, 'username' => 'a5'),
);
我们要提取其中的 usename 列,变成:
$username = array('a1', 'a2', 'a3', 'a4', 'a5');
则用:
$username = array_column($user, 'username');
--------------------------------------------------------------------------
获取数组某元素出现的次数array_count_values()
例子:
$arr = array('Ann','David','Hebe','David');
则有
array('Ann' => 1,'David' => 2, 'Hebe' => 1) = array_count_values($arr);
--------------------------------------------------------------------------
给数组$array 排序:按field 的倒序
$data = array();
foreach( $array as $key => $value){
$data[$key] = $value['field'];
}
array_multisort$data,SORT_DESC,$array);
--------------------------------------------------------------------------
获取数组$array中某列$field元素出现的次数
//获取数组中某个字段的值出现次数
public function count_array($array,$field){
$ids = array_column($array,$field);
$arr = array_count_values($ids);
return $arr;
}
---------------------------------------------------------------
一键复制
编辑
Web IDE
原始数据
按行查看
历史