/**
* Desc:二维数组按指定字段排序
* Author:glt 2023/6/30 14:07
*
* @param $array
* @param $field
* @param string $order
*
* @return array
*/
function twoArraySort($array, $field, $order = 'ASC')
{
if(!$array || !$field){
return [];
}
$order = strtoupper($order);
if(!in_array($order, ['ASC', 'DESC'])){
$order = 'DESC';
}
$sort = [
'direction' => 'SORT_' . $order, //排序顺序标志 SORT_DESC 降序;SORT_ASC 升序
'field' => $field, //排序字段
];
$arrSort = [];
foreach($array as $uniqid => $row){
foreach($row as $key => $value){
$arrSort[$key][$uniqid] = $value;
}
}
if($sort['direction']){
array_multisort($arrSort[$sort['field']], constant($sort['direction']), $array);
}
return $array;
}
/**
* Desc:正则匹配,从字符串里取数字
* Author:glt 2023/11/4 18:29
* @param $str
*
* @return string|string[]|null
*/
function strFindNum($str)
{
return preg_replace('/\D/s', '', $str);
}
/**
* Desc:http请求get方法
* Author:glt 2023/6/30 11:43
*
* @param string $url
* @param array $query
*
* @return bool|string
*/
function http_get(string $url, array $query = [])
{
if(!empty($query)){
$url = $url . '?' . http_build_query($query);
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$header = [];
$header[] = 'Content-Type: application/json;charset=UTF-8';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
return curl_exec($ch);
}
/**
* Desc:http请求post方法
* Author:glt 2023/6/30 10:10
*
* @param string $url
* @param array $data
* @param array $query
*
* @return bool|string
*/
function http_post(string $url, array $data = [], array $query = [])
{
$post_data = json_encode($data, 256);
if(!empty($query)){
$url = $url . '?' . http_build_query($query);
}
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$header = [];
$header[] = 'Content-Type: application/json;charset=UTF-8';
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
return curl_exec($ch);
}
- mongo写入(在框架不支持mongo时,自己对接扩展方法)
/**
* Desc:mongo写入数据
* Author:glt 2023/6/28 16:27
*
* @param mixed $data 要写入的数据
* @param string $table_name 写入的集合名称
*/
function mongo_insert($data, $table_name)
{
$manager = new MongoDB\Driver\Manager("mongodb://localhost:27017");
$bulkWriteManager = new MongoDB\Driver\BulkWrite();
[$t1, $t2] = explode(' ', microtime());
$million_second = (float)sprintf('%.0f', (floatval($t1 + 3600 * 8) + floatval($t2)) * 1000);
$time = new \MongoDB\BSON\UTCDateTime($million_second);
$data['add_time'] = $time;
$bulkWriteManager->insert($data);
$manager->executeBulkWrite('vekise.' . $table_name, $bulkWriteManager);
}
/**
* Desc:生成唯一id
* Author:glt 2023/7/12 18:04
* @return string
*/
function createUniqueId()
{
$chars = md5(uniqid(mt_rand(), TRUE));
return substr($chars, 0, 8) . '-'
. substr($chars, 8, 4) . '-'
. substr($chars, 12, 4) . '-'
. substr($chars, 16, 4) . '-'
. substr($chars, 20, 12) . time();
}
/**
* Desc:无限极分类生成树状结构
* Author:glt 2024/1/3 16:19
* @param $data
* @return array
*/
public function generateTree($data)
{
$items = array_column($data, null, 'id');
$tree = [];
foreach ($items as $k => $item) {
if (isset($items[$item['pid']])) {
$items[$item['pid']]['child'][] = &$items[$k];
} else {
$tree[] = &$items[$k];
}
}
return $tree;
}
/**
* Desc:无限极分类寻找祖先节点
* Author:glt 2024/1/19 9:46
* @param $data
* @param $targetId
* @return array
*/
function getParents($data, $targetId)
{
// 存放目标节点以及其祖先节点的数组
$result = [];
foreach ($data as $category) {
if ($category['pid'] == null && $category['id'] != $targetId) {
continue;
}
if ($category['id'] == $targetId || in_array($category['id'], array_column($result, 'id'))) {
$result[] = $category['id'];
if ($category['pid']) {
$children = $this->getParents($data, $category['pid']);
if (!empty($children)) {
$result = array_merge($result, $children);
}
}
}
}
return $result;
}
/**
* Desc:无限极分类返回子集
* Author:glt 2024/3/6 17:26
* @param $data
* @param $pid
* @return array
*/
public function getChildren($data, $pid)
{
$arr = array();
foreach ($data as $v) {
if ($v['pid'] == $pid) {
$arr[] = $v;
$arr = array_merge($arr, $this->getChildren($data, $v['id']));
}
}
return $arr;
}