| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607 |
- <?php
- namespace common\helpers;
- use yii\helpers\BaseArrayHelper;
- use yii\helpers\Json;
- /**
- * Class ArrayHelper
- * @package common\helpers
- * @author qimall
- */
- class ArrayHelper extends BaseArrayHelper
- {
- /**
- * 递归数组
- *
- * @param array $items
- * @param string $idField
- * @param int $pid
- * @param string $pidField
- * @return array
- */
- public static function itemsMerge(array $items, $pid = 0, $idField = "id", $pidField = 'pid', $child = '-')
- {
- $map = [];
- $tree = [];
- foreach ($items as &$it) {
- $it[$child] = [];
- $map[$it[$idField]] = &$it;
- }
- foreach ($items as &$it) {
- $parent = &$map[$it[$pidField]];
- if ($parent) {
- $parent[$child][] = &$it;
- } else {
- $pid == $it[$pidField] && $tree[] = &$it;
- }
- }
- unset($items, $map);
- return $tree;
- }
- /**
- * 传递一个子分类ID返回所有的父级分类
- *
- * @param array $items
- * @param $id
- * @return array
- */
- public static function getParents(array $items, $id)
- {
- $arr = [];
- foreach ($items as $v) {
- if ($v['id'] == $id) {
- $arr[] = $v;
- $arr = array_merge(self::getParents($items, $v['pid']), $arr);
- }
- }
- return $arr;
- }
- /**
- * 传递一个父级分类ID返回所有子分类
- *
- * @param $cate
- * @param int $pid
- * @return array
- */
- public static function getChilds($cate, $pid)
- {
- $arr = [];
- foreach ($cate as $v) {
- if ($v['pid'] == $pid) {
- $arr[] = $v;
- $arr = array_merge($arr, self::getChilds($cate, $v['id']));
- }
- }
- return $arr;
- }
- /**
- * 传递一个父级分类ID返回所有子分类ID
- *
- * @param $cate
- * @param $pid
- * @param string $idField
- * @param string $pidField
- * @return array
- */
- public static function getChildIds($cate, $pid, $idField = "id", $pidField = 'pid')
- {
- $arr = [];
- foreach ($cate as $v) {
- if ($v[$pidField] == $pid) {
- $arr[] = $v[$idField];
- $arr = array_merge($arr, self::getChildIds($cate, $v[$idField], $idField, $pidField));
- }
- }
- return $arr;
- }
- /**
- * php二维数组排序 按照指定的key 对数组进行排序
- *
- * @param array $arr 将要排序的数组
- * @param string $keys 指定排序的key
- * @param string $type 排序类型 asc | desc
- * @return array
- */
- public static function arraySort($arr, $keys, $type = 'asc')
- {
- if (count($arr) <= 1) {
- return $arr;
- }
- $keysValue = [];
- $newArray = [];
- foreach ($arr as $k => $v) {
- $keysValue[$k] = $v[$keys];
- }
- $type == 'asc' ? asort($keysValue) : arsort($keysValue);
- reset($keysValue);
- foreach ($keysValue as $k => $v) {
- $newArray[$k] = $arr[$k];
- }
- return $newArray;
- }
- public static function arraySorts($arr, $keys, $type = 'asc')
- {
- if (count($arr) <= 1) {
- return $arr;
- }
- $keysValue = [];
- $newArray = [];
- foreach ($arr as $k => $v) {
- $keysValue[$k] = $v[$keys];
- }
- $type == 'asc' ? asort($keysValue) : arsort($keysValue);
- reset($keysValue);
- foreach ($keysValue as $k => $v) {
- $newArray[] = $arr[$k];
- }
- return $newArray;
- }
- /**
- * 获取数组指定的字段为key
- *
- * @param array $arr 数组
- * @param string $field 要成为key的字段名
- * @return array
- */
- public static function arrayKey($arr, $field)
- {
- $newArray = [];
- if (empty($arr)) {
- return $newArray;
- }
- foreach ($arr as $value) {
- isset($value[$field]) && $newArray[$value[$field]] = $value;
- }
- return $newArray;
- }
- /**
- * 移除数组内某个key的值为传递的值
- *
- * @param array $array
- * @param $value
- * @param string $key
- * @return array
- */
- public static function removeByValue(array $array, $value, $key = 'id')
- {
- foreach ($array as $index => $item) {
- if ($item[$key] == $value) {
- unset($array[$index]);
- }
- }
- return $array;
- }
- /**
- * 获取数字区间
- *
- * @param int $start
- * @param int $end
- * @return array
- */
- public static function numBetween($start = 0, $end = 1, $key = true, $step_number = 1)
- {
- $arr = [];
- for ($i = $start; $i <= $end; $i = $i + $step_number) {
- $key == true ? $arr[$i] = $i : $arr[] = $i;
- }
- return $arr;
- }
- /**
- * 根据级别和数组返回字符串
- *
- * @param int $level 级别
- * @param array $models
- * @param $k
- * @param int $treeStat 开始计算
- * @return bool|string
- */
- public static function itemsLevel($level, array $models, $k, $treeStat = 1)
- {
- $str = '';
- for ($i = 1; $i < $level; $i++) {
- $str .= ' ';
- if ($i == $level - $treeStat) {
- if (isset($models[$k + 1])) {
- return $str . "├──";
- }
- return $str . "└──";
- }
- }
- return false;
- }
- /**
- * 必须经过递归才能进行重组为下拉框
- *
- * @param $models
- * @param string $idField
- * @param string $titleField
- * @param int $treeStat
- * @return array
- */
- public static function itemsMergeDropDown($models, $idField = 'id', $titleField = 'title', $treeStat = 1)
- {
- $arr = [];
- foreach ($models as $k => $model) {
- $arr[] = [
- $idField => $model[$idField],
- $titleField => self::itemsLevel($model['level'], $models, $k, $treeStat) . " " . $model[$titleField],
- ];
- if (!empty($model['-'])) {
- $arr = ArrayHelper::merge(
- $arr,
- self::itemsMergeDropDown($model['-'], $idField, $titleField, $treeStat)
- );
- }
- }
- return $arr;
- }
- /**
- * 匹配ip在ip数组内支持通配符
- *
- * @param $ip
- * @param $allowedIPs
- * @return bool
- */
- public static function ipInArray($ip, $allowedIPs)
- {
- foreach ($allowedIPs as $filter) {
- if ($filter === '*' || $filter === $ip || (($pos = strpos($filter, '*')) !== false && !strncmp($ip, $filter, $pos))) {
- return true;
- }
- }
- return false;
- }
- /**
- * 对比2组id,返回存在的id和被删除的id
- *
- * @param array $oldIds
- * @param array $newIds
- * @return array
- */
- public static function comparisonIds(array $oldIds, array $newIds)
- {
- $updatedIds = $deleteIds = [];
- foreach ($oldIds as $oldId) {
- if (in_array($oldId, $newIds)) {
- $updatedIds[] = $oldId;
- } else {
- $deleteIds[] = $oldId;
- }
- }
- return [$updatedIds, $deleteIds];
- }
- /**
- * 获取递归的第一个没有子级的数据
- *
- * @param $array
- * @return mixed
- */
- public static function getFirstRowByItemsMerge(array $array)
- {
- foreach ($array as $item) {
- if (!empty($item['-'])) {
- return self::getFirstRowByItemsMerge($item['-']);
- } else {
- return $item;
- }
- }
- return false;
- }
- /**
- * 获取所有没有子级的数据
- *
- * @param $array
- * @return mixed
- */
- public static function getNotChildRowsByItemsMerge(array $array)
- {
- $arr = [];
- foreach ($array as $item) {
- if (!empty($item['-'])) {
- $arr = array_merge($arr, self::getNotChildRowsByItemsMerge($item['-']));
- } else {
- $arr[] = $item;
- }
- }
- return $arr;
- }
- /**
- * 递归转普通二维数组
- *
- * @param $array
- * @return mixed
- */
- public static function getRowsByItemsMerge(array $array, $childField = '-')
- {
- $arr = [];
- foreach ($array as $item) {
- if (!empty($item[$childField])) {
- $arr = array_merge($arr, self::getRowsByItemsMerge($item[$childField]));
- }
- unset($item[$childField]);
- $arr[] = $item;
- }
- return $arr;
- }
- /**
- * 重组 map 类型转为正常的数组
- *
- * @param array $array
- * @param string $keyForField
- * @param string $valueForField
- * @return array
- */
- public static function regroupMapToArr($array = [], $keyForField = 'route', $valueForField = 'title')
- {
- $arr = [];
- foreach ($array as $key => $item) {
- if (!is_array($array[$key])) {
- $arr[] = [
- $keyForField => $key,
- $valueForField => $item,
- ];
- } else {
- $arr[] = $item;
- }
- }
- return $arr;
- }
- /**
- * 数组内某字段转数组
- *
- * @param array $data
- * @param string $field
- * @return array
- */
- public static function fieldToArray(array $data, $field = 'covers')
- {
- foreach ($data as &$datum) {
- if (empty($datum[$field])) {
- $datum[$field] = [];
- }
- if (!is_array($datum[$field])) {
- $datum[$field] = Json::decode($datum[$field]);
- }
- }
- return $data;
- }
- /**
- * 数组转xml
- *
- *
- * @param $arr
- * 微信回调成功:['return_code' => 'SUCCESS', 'return_msg' => 'OK']
- * 微信回调失败:['return_code' => 'FAIL', 'return_msg' => 'OK']
- * @return bool|string
- */
- public static function toXml($arr)
- {
- if (!is_array($arr) || count($arr) <= 0) {
- return false;
- }
- $xml = "<xml>";
- foreach ($arr as $key => $val) {
- if (is_numeric($val)) {
- $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
- } else {
- $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
- }
- }
- $xml .= "</xml>";
- return $xml;
- }
- public static function get_client_ip()
- {
- if (!empty($_SERVER['REMOTE_ADDR'])) {
- $ip = $_SERVER['REMOTE_ADDR'];
- } else {
- // for php-cli(phpunit etc.)
- $ip = defined('PHPUNIT_RUNNING') ? '127.0.0.1' : gethostbyname(gethostname());
- }
- return filter_var($ip, FILTER_VALIDATE_IP) ?: '127.0.0.1';
- }
- /**
- * 排列组合
- * 采用二进制方法进行组合的选择,如表示5选3时,只需有3位为1就可以了,所以可得到的组合是 01101 11100 00111 10011 01110等10种组合
- *
- * @param array $arr 需要排列的数组
- * @param integer $min_size 最小个数
- * @return array 满足条件的新数组组合
- */
- public static function pl($arr, $size = 5)
- {
- $len = count($arr);
- $max = pow(2, $len);
- $min = pow(2, $size) - 1;
- $r_arr = array();
- for ($i = $min; $i < $max; $i++) {
- $count = 0;
- $t_arr = array();
- for ($j = 0; $j < $len; $j++) {
- $a = pow(2, $j);
- $t = $i & $a;
- if ($t == $a) {
- $t_arr[] = $arr[$j];
- $count++;
- }
- }
- if ($count == $size) {
- $r_arr[] = $t_arr;
- }
- }
- return $r_arr;
- }
- /**
- * N个数组的笛卡尔集
- * @Author bing
- * @DateTime 2021-09-03 15:06:19
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @return array
- */
- public static function combineDika()
- {
- $data = func_get_args();
- $data = current($data);
- $result = array();
- $arr1 = array_shift($data);
- foreach ($arr1 as $key => $item) {
- $result[] = array($item);
- }
- foreach ($data as $key => $item) {
- $result = self::combineArray($result, $item);
- }
- return $result;
- }
- /**
- * 两个数组的笛卡尔积
- * @Author bing
- * @DateTime 2021-09-03 15:07:11
- * @copyright: Copyright (c) 2020 广东七件事集团
- * @param [type] $arr1
- * @param [type] $arr2
- * @return array
- */
- public static function combineArray($arr1, $arr2)
- {
- $result = array();
- foreach ($arr1 as $item1) {
- foreach ($arr2 as $item2) {
- $temp = $item1;
- $temp[] = $item2;
- $result[] = $temp;
- }
- }
- return $result;
- }
- /**
- * 二维数组组合为树形结构
- * @Author: lun
- * @DateTime: 2023/8/4 0004 10:13
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param array $arrays 数组
- * @param string $idField 子字段
- * @param string $pidField 父字段
- * @param string $child 组合的子参数字段
- * @return array
- */
- public static function buildTree($arrays, $idField = "id", $pidField = 'pid', $child = '-') {
- $tree = array();
- $isset_key = [];
- foreach ($arrays as $item) {
- $tree[$item[$idField]] = $item;
- }
- foreach ($tree as $key => $item) {
- if ($item[$pidField] && isset($tree[$item[$pidField]])) {
- $isset_key[] = $key;
- $tree[$item[$pidField]][$child][] = &$tree[$key];
- }
- }
- foreach ($tree as $key => $item) {
- if (in_array($key, $isset_key)) unset($tree[$key]);
- if ($item[$pidField] && isset($tree[$item[$pidField]])) {
- unset($tree[$key]);
- }
- }
- return $tree;
- }
- /**
- * 去除重复项
- * @param array $list
- * @param $field
- * @return array
- */
- public static function removeDuplicates($list, $field)
- {
- if (!empty($list)) {
- $tmp = [];
- $list = (array) $list;
- foreach ($list as $key => $item) {
- if (is_array($item) && isset($item[$field])) {
- if (in_array($item[$field], $tmp)) {
- unset($list[$key]);
- } else {
- $tmp[] = $item[$field];
- }
- }
- }
- }
- return $list;
- }
- /**
- * 数组查找
- *
- * @param array $array
- * @param \Closure $callback
- * @return mixed
- */
- public static function find(array $array, \Closure $callback)
- {
- $result = array_filter($array, $callback);
- return reset($result); // 返回匹配到的第一个元素
- }
- }
|