PHP 常用方法 (二)

无限级分类(一维数组)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* 无限级分类(一维数组)
* @param array $data
* @param array $fieldNameArr
* @param int $pid
* @param int $level
* @param int $showLevel
*
* @return array
*/
function getTree(&$data, $fieldNameArr = ['pid', 'id'], $pid = 0, $level = 0, $showLevel = 3)
{
if ($level >= $showLevel) {
return $data['new'];
}

if (!isset($data['old'])) {
$data = [
'old' => $data,
'new' => []
];
}

foreach ($data['old'] as $k => $v) {
if ($v[$fieldNameArr[0]] == $pid) {
$v['level'] = $level;
$data['new'][] = $v;
unset($data['old'][$k]);
getTree($data, $fieldNameArr, $v[$fieldNameArr[1]], $level + 1, $showLevel);
}
}
return $data['new'];
}

无限级分类(多维数组)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* 无限级分类(多维数组)
*
* @param array $data
* @param array $fieldNameArr
*
* @return array
*/
function getTreeT($data, $fieldNameArr = ['pid', 'id'])
{
$dataArr = [];
foreach ($data as $d) {
$dataArr[$d[$fieldNameArr[1]]] = $d;
$dataArr[$d[$fieldNameArr[1]]]['child'] = array();
}
foreach ($dataArr as $k => $v) {
if ($v[$fieldNameArr[0]] != 0) {
$dataArr[$v[$fieldNameArr[0]]]['child'][$v[$fieldNameArr[1]]] = &$dataArr[$v[$fieldNameArr[1]]];
}
}
foreach ($dataArr as $tk => $tv) {
if ($tv[$fieldNameArr[0]] != 0 || !isset($tv[$fieldNameArr[0]])) {
unset($dataArr[$tk]);
}
}

return $dataArr;
}

获取某个分类下的所有子类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* 获取某个分类下的所有子类
* @param array $data
* @param int $parentId
* @param array $fieldNameArr
* @return array $dataArr
*/
function getCateChild($data, $parentId, $fieldNameArr = ['pid', 'id'], &$dataArr = array())
{
foreach ($data as $k => $v) {
if ($v[$fieldNameArr[0]] == $parentId) {
$dataArr[] = $v[$fieldNameArr[1]];
getCateChild($data, $v[$fieldNameArr[1]], $fieldNameArr, $dataArr);
}
}

return $dataArr;
}

IP转换为整数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/**
* IP转换为整数
* IP段地址:http://ipblock.chacuo.net/down/t_txt=c_CN
*
* @param boolean $rand 获取国内随机IP
*
* @return array
*/
function get_ip_convert($rand = false)
{
if ($rand) {
$ipLong = [
['610271232', '618659839'], // 36.96.0.0 - 36.223.255.255
['1002434560', '1008730111'], // 59.191.240.0 - 60.31.255.255
['1861222400', '1866465279'], // 110.240.0.0 - 111.63.255.255
['-569376768', '-564133889'], // 222.16.0.0 - 222.95.255.255
['-1224736768', '-1220018177'], // 183.0.0.0 - 183.71.255.255
['-1212678144', '-1207959553'], // 183.184.0.0 - 183.255.255.255
['1879048192', '1883504639'], // 112.0.0.0 - 112.67.255.255
];
$randKey = mt_rand(0, count($ipLong));
$ipArr = [long2ip(mt_rand($ipLong[$randKey][0], $ipLong[$randKey][1]))];
} else {
$ipArr = [$_SERVER['REMOTE_ADDR'] == '::1' ? '127.0.0.1' : $_SERVER['REMOTE_ADDR']];
}

$ipArr[1] = sprintf('%u', ip2long($ipArr[0]));
return $ipArr;
}

笛卡尔积

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
function cartesianProduct(array $params): array
{
$result = array_shift($params);
while ($item = array_shift($params)) {
$temp = [];
foreach ($result as $res) {
!is_array($res) && $res = [$res];
foreach ($item as $val) {
!is_array($val) && $val = [$val];
$temp[] = array_merge($res, $val);
}
}

$result = $temp;
}

return $result;
}

客户端类型校验

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
/**
* 客户端类型校验
*
* @param int $type 1:微信内置浏览器, 2:支付宝内置浏览器, 3:IOS, 4:Android
*
* @return bool
*/
function clientTypeValidator(int $type): bool
{
$mobileFlag = self::isMobile();
if ($mobileFlag) {
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
switch ($type) {
// 微信内置浏览器
case 1:
return strpos($userAgent, 'micromessenger') !== false;
// 支付宝内置浏览器
case 2:
return strpos($userAgent, 'aliapp') !== false;
// IOS
case 3:
return strpos($userAgent, 'iphone') !== false;
// Android
case 4:
return strpos($userAgent, 'android') !== false;
default:
return false;
}
}

return false;
}

判断请求来源是否手机端

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/**
* 判断请求来源是否手机端
*
* @return int
*/
function isMobile()
{
// HTTP_X_WAP_PROFILE|VIA信息含有wap
if (isset($_SERVER['HTTP_X_WAP_PROFILE']) || (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], 'wap'))) {
return true;
}

$clientKeywords = [
'nokia', 'sony', 'ericsson', 'mot', 'samsung', 'htc', 'sgh', 'lg', 'sharp', 'sie-', 'philips', 'panasonic', 'alcatel', 'lenovo', 'iphone',
'ipod', 'blackberry', 'meizu', 'android', 'netfront', 'symbian', 'ucweb', 'windowsce', 'palm', 'operamini', 'operamobi', 'openwave',
'nexusone', 'cldc', 'midp', 'wap', 'mobile'
];
if (preg_match("/(" . implode('|', $clientKeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
return true;
}

return false;
}

JSON字符串校验

1
2
3
4
5
6
7
8
9
10
11
12
/**
* JSON字符串校验
*
* @param string $jsonStr
*
* @return false|array
*/
function jsonValidate(string $jsonStr)
{
$result = json_decode($jsonStr, true);
return (json_last_error() == JSON_ERROR_NONE) ? $result : false;
}
0%