PHPでよく使われるキックアスの天揚げのテクニック(一)
10111 ワード
ほとんどの2、3年の経験を持つPHPerは、日常開発で使われる小さなテクニックを蓄積しているに違いない.フロントエンド、インタフェースの開発、チームのプログラミング規範、モジュール化と先輩たちが共有した個人的な経験から、いくつかの小さなテクニックをまとめました.これらの技能はすべて大いに私达の日常の开発の効率を高めることができて、私达のコードの质を高めて、更にあなたに多くの本を読むことを省くことができて、ブログを见て、妹を探しておしゃべりをする时間を省くことができます.以下は私個人が仕事の経験からまとめたいくつかのプログラミングのテクニックで、その中には先輩たちが分かち合った経験も含まれているかもしれません.もしもっと良い実現方法があれば、伝言を残して貴重な経験を分かち合って、次の分かち合いや後続の中でさらに補充することができます.
一.一般的な関数/メソッド:
関数といえば、私の话は多くなって、PHPの小さい技巧を分かち合う中で、みんながよく知っているPHP関数がたくさんあって、それでは日常の仕事の中でよく使うみんなもあなたのプロジェクトのコードから离れられないことがたくさんあって、あなたのfunctionから离れられません.phpまたはcommont.php関数ライブラリです.以下は私のよく使う関数で、皆さんに共有します.
1.ハイライト表示されたブレークポイントデバッグツール(ブレークポイントとbackgroupに限定されない柔軟性があります):
一.一般的な関数/メソッド:
関数といえば、私の话は多くなって、PHPの小さい技巧を分かち合う中で、みんながよく知っているPHP関数がたくさんあって、それでは日常の仕事の中でよく使うみんなもあなたのプロジェクトのコードから离れられないことがたくさんあって、あなたのfunctionから离れられません.phpまたはcommont.php関数ライブラリです.以下は私のよく使う関数で、皆さんに共有します.
1.ハイライト表示されたブレークポイントデバッグツール(ブレークポイントとbackgroupに限定されない柔軟性があります):
function debug($data){
if(empty($data)){
var_dump($data);
die;
}
if(!is_array($data)){
echo "";
echo "";
print_r($data);
echo "";
echo "
";
die;
}
echo "";
echo "
";
print_r($data);
echo "
";
echo "
";
die;
}
2. (データベース が または に かれたゴミコードを として する):/* @param $data array
* @param $pid int
* @param $parentFieldstring
* @param $pkField string
* return array
*/
function getTreesPro($data,$pid='0',$parentField='pid',$pkField='id'){
$tree =array();
foreach($data as $k=>$v){
if($v[$parentField] == $pid){
$temp = getTreesPro($data,$v[$pkField]);//$data $v->$pkField
if(!empty($temp)){
//
$v['son']= getTreesPro($data,$v[$pkField]);
}
$tree[] = $v;
}
}
return $tree;
}
3. オブジェクトfunction arrayToObject($arr){
if(is_array($arr)){
return (object) array_map(__FUNCTION__, $arr);
}else{
return $arr;
}
}
4.オブジェクトの function object2array(&$object) {
$object = json_decode( json_encode( $object),true);
return $object;
}
5. オーダー の function generateJnlNo() {
date_default_timezone_set('PRC');
$yCode = array('A','B','C','D','E','F','G','H','I','J');
$orderSn = '';
$orderSn .= $yCode[(intval(date('Y')) - 1970) % 10];
$orderSn .= strtoupper(dechex(date('m')));
$orderSn .= date('d').substr(time(), -5);
$orderSn .= substr(microtime(), 2, 5);
$orderSn .= sprintf('%02d', mt_rand(0, 99));
//echo $orderSn,PHP_EOL; // :G107347128750079
return $orderSn;
}
6.2 D をHashMapに し、 を します./**
* 1:
* @code php
* $rows = array(
* array('id' => 1, 'value' => '1-1'),
* array('id' => 2, 'value' => '2-1'),
*);
* $hashmap = Helper_Array::hashMap($rows, 'id', 'value');
*
* dump($hashmap);
* //
* // array(
* // 1 => '1-1',
* // 2 => '2-1',
* //)
* @endcode
*
* $value_field , 。
*
* 2:
* @code php
* $rows = array(
* array('id' => 1, 'value' => '1-1'),
* array('id' => 2, 'value' => '2-1'),
*);
* $hashmap = Helper_Array::hashMap($rows, 'id');
*
* dump($hashmap);
* //
* // array(
* // 1 => array('id' => 1, 'value' => '1-1'),
* // 2 => array('id' => 2, 'value' => '2-1'),
* //)
* @endcode
*
* @param array $arr
* @param string $key_field
* @param string $value_field
*
* @return array HashMap
*/
function to_hashmap($arr, $key_field, $value_field = null){
$ret = array();
if ($value_field){
foreach ($arr as $row){
$ret[$row[$key_field]] = $row[$value_field];
}
}
else{
foreach ($arr as $row){
$ret[$row[$key_field]] = $row;
}
}
return $ret;
}
7.2ビット から、あるフィールドのすべての ( を む)を り す
$brandListデータからすべてのidの を り す :$ids=array_column($brandList,'id'); デリバリー $ids=array_unique(array_column($brandList,'id')); if (!function_exists('array_column')) {
/**
* Returns the values from a single column of the input array, identified by
* the $columnKey.
*
* Optionally, you may provide an $indexKey to index the values in the returned
* array by the values from the $indexKey column in the input array.
*
* @param array $input A multi-dimensional array (record set) from which to pull
* a column of values.
* @param mixed $columnKey The column of values to return. This value may be the
* integer key of the column you wish to retrieve, or it
* may be the string key name for an associative array.
* @param mixed $indexKey (Optional.) The column to use as the index/keys for
* the returned array. This value may be the integer key
* of the column, or it may be the string key name.
* @return array
*/
function array_column($input = null, $columnKey = null, $indexKey = null){
// Using func_get_args() in order to check for proper number of
// parameters and trigger errors exactly as the built-in array_column()
// does in PHP 5.5.
$argc = func_num_args();
$params = func_get_args();
if ($argc < 2) {
trigger_error("array_column() expects at least 2 parameters, {$argc} given", E_USER_WARNING);
return array();
}
if (!is_array($params[0])) {
trigger_error('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING);
return array();
}
if (!is_int($params[1])
&& !is_float($params[1])
&& !is_string($params[1])
&& $params[1] !== null
&& !(is_object($params[1]) && method_exists($params[1], '__toString'))
) {
trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
return array();
}
if (isset($params[2])
&& !is_int($params[2])
&& !is_float($params[2])
&& !is_string($params[2])
&& !(is_object($params[2]) && method_exists($params[2], '__toString'))
) {
trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
return array();
}
$paramsInput = $params[0];
$paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
$paramsIndexKey = null;
if (isset($params[2])) {
if (is_float($params[2]) || is_int($params[2])) {
$paramsIndexKey = (int) $params[2];
} else {
$paramsIndexKey = (string) $params[2];
}
}
$resultArray = array();
foreach ($paramsInput as $row) {
$key = $value = null;
$keySet = $valueSet = false;
if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
$keySet = true;
$key = (string) $row[$paramsIndexKey];
}
if ($paramsColumnKey === null) {
$valueSet = true;
$value = $row;
} elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
$valueSet = true;
$value = $row[$paramsColumnKey];
}
if ($valueSet) {
if ($keySet) {
$resultArray[$key] = $value;
} else {
$resultArray[] = $value;
}
}
}
return array_unique($resultArray);
}
}
8.クライアントキャッシュ public function cache($seconds_to_cache = 3600){
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: $ts");
header("Pragma: cache");
header("Cache-Control: max-age=$seconds_to_cache");
}
9.クライアントがキャッシュしない public function disCache(){
$ts = gmdate("D, d M Y H:i:s",strtotime('-1 year')) . " GMT";
header("Expires: $ts");
header("Last-Modified: $ts");
header("Pragma: no-cache");
header("Cache-Control: no-cache, must-revalidate");
}
10. のページのソースに るpublic function referer(){
return $_SERVER['HTTP_REFERER'];
}
11.ページング (apiで く われている)public function pageinfo(){
$pageinfo = new \stdClass;
$pageinfo->length = isset($_GET['length']) ? $_GET['length'] : $this->length;
$pageinfo->page = isset($_GET['page']) ? $_GET['page'] : 1;
$pageinfo->end_id = isset($_GET['end_id']) ? $_GET['end_id'] : 1;
$pageinfo->offset= $pageinfo->page<=1 ? 0 : ($pageinfo->page-1) * $pageinfo->length;
$pageinfo->totalNum = $pageinfo->totalNum? $pageinfo->totalNum : 0;
$pageinfo->totalPage = $pageinfo->totalNum / $pageinfo->length;
return $pageinfo;
}
の の のため、 にゆっくりとみんなに かち います!もしあなたが くの いテクニックがあれば、 の で を して、 け った にすぐにあなたの に します!ご ありがとうございます!