* Move methed to base.

This commit is contained in:
caoyanyi
2023-09-05 12:20:43 +08:00
parent 29ba3490c6
commit 3ea5fc3d4e
2 changed files with 61 additions and 61 deletions
+61 -1
View File
@@ -901,7 +901,7 @@ class baseHelper
global $config;
return $config->cache->enable;
}
/**
* Generate rand string.
*
@@ -914,6 +914,66 @@ class baseHelper
$seeds = str_shuffle('abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ123456789');
return substr($seeds, 0, $length);
}
/**
* 获取二维数组中的某一列。
* Get array column to array.
*
* @param array $input
* @param int|string|null $columnKey
* @param int|string|null $indexKey
* @static
* @access public
* @return array
*/
public static function arrayColumn(array $input, $columnKey, $indexKey = null): array
{
/* If php version greater than 7, calling system functions returns. */
if(defined(PHP_VERSION_ID) && PHP_VERSION_ID >= 70000) return \array_column($input, $columnKey, $indexKey);
$output = array();
foreach($input as $row)
{
$key = $value = null;
$keySet = $valueSet = false;
if($indexKey !== null && array_key_exists($indexKey, (array) $row))
{
$keySet = true;
$key = \is_object($row) ? (string) $row->$indexKey : (string) $row[$indexKey];
}
if(null === $columnKey)
{
$valueSet = true;
$value = $row;
}
elseif(\is_array($row) && \array_key_exists($columnKey, $row))
{
$valueSet = true;
$value = $row[$columnKey];
}
elseif(\is_object($row) && \property_exists($row, $columnKey))
{
$valueSet = true;
$value = $row->$columnKey;
}
if($valueSet)
{
if($keySet)
{
$output[$key] = $value;
}
else
{
$output[] = $value;
}
}
}
return $output;
}
}
//------------------------------- 常用函数。Some tool functions.-------------------------------//
-60
View File
@@ -409,66 +409,6 @@ class helper extends baseHelper
return (string)$value;
}
}
/**
* 获取二维数组中的某一列。
* Get array column to array.
*
* @param array $input
* @param int|string|null $columnKey
* @param int|string|null $indexKey
* @static
* @access public
* @return array
*/
public static function arrayColumn(array $input, $columnKey, $indexKey = null): array
{
/* If php version greater than 7, calling system functions returns. */
if(defined(PHP_VERSION_ID) && PHP_VERSION_ID >= 70000) return \array_column($input, $columnKey, $indexKey);
$output = array();
foreach($input as $row)
{
$key = $value = null;
$keySet = $valueSet = false;
if($indexKey !== null && array_key_exists($indexKey, (array) $row))
{
$keySet = true;
$key = \is_object($row) ? (string) $row->$indexKey : (string) $row[$indexKey];
}
if(null === $columnKey)
{
$valueSet = true;
$value = $row;
}
elseif(\is_array($row) && \array_key_exists($columnKey, $row))
{
$valueSet = true;
$value = $row[$columnKey];
}
elseif(\is_object($row) && \property_exists($row, $columnKey))
{
$valueSet = true;
$value = $row->$columnKey;
}
if($valueSet)
{
if($keySet)
{
$output[$key] = $value;
}
else
{
$output[] = $value;
}
}
}
return $output;
}
}
/**