截流自动化的商城平台
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ArrayServer.php 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | likeshop开源商城系统
  4. // +----------------------------------------------------------------------
  5. // | 欢迎阅读学习系统程序代码,建议反馈是我们前进的动力
  6. // | gitee下载:https://gitee.com/likeshop_gitee
  7. // | github下载:https://github.com/likeshop-github
  8. // | 访问官网:https://www.likeshop.cn
  9. // | 访问社区:https://home.likeshop.cn
  10. // | 访问手册:http://doc.likeshop.cn
  11. // | 微信公众号:likeshop技术社区
  12. // | likeshop系列产品在gitee、github等公开渠道开源版本可免费商用,未经许可不能去除前后端官方版权标识
  13. // | likeshop系列产品收费版本务必购买商业授权,购买去版权授权后,方可去除前后端官方版权标识
  14. // | 禁止对系统程序代码以任何目的,任何形式的再发布
  15. // | likeshop团队版权所有并拥有最终解释权
  16. // +----------------------------------------------------------------------
  17. // | author: likeshop.cn.team
  18. // +----------------------------------------------------------------------
  19. namespace app\common\server;
  20. /**
  21. * 数组转换 服务类
  22. * Class ArrayServer
  23. * @Author FZR
  24. * @package app\common\basics
  25. */
  26. class ArrayServer
  27. {
  28. /**
  29. * Notes: 线性结构转换成树形结构
  30. * @param $data 线行结构数组
  31. * @param string $sub_key_name 自动生成子数组名
  32. * @param string $id_name 数组id名
  33. * @param string $parent_id_name 数组祖先id名
  34. * @param int $parent_id 此值请勿给参数
  35. * @return array
  36. * @author 段誉(2021/4/9 15:12)
  37. */
  38. public static function linear_to_tree($data, $sub_key_name = 'sub', $id_name = 'id', $parent_id_name = 'pid', $parent_id = 0)
  39. {
  40. $tree = [];
  41. foreach ($data as $row) {
  42. if ($row[$parent_id_name] == $parent_id) {
  43. $temp = $row;
  44. $temp[$sub_key_name] = self::linear_to_tree($data, $sub_key_name, $id_name, $parent_id_name, $row[$id_name]);
  45. $tree[] = $temp;
  46. }
  47. }
  48. return $tree;
  49. }
  50. /**
  51. * Notes: 多级线性结构排序
  52. * @param $data
  53. * @param string $symbol
  54. * @param string $name
  55. * @param string $id_name
  56. * @param string $parent_id_name
  57. * @param int $level
  58. * @param int $parent_id
  59. * @return array
  60. * * 转换前:
  61. * [{"id":1,"pid":0,"name":"a"},{"id":2,"pid":0,"name":"b"},{"id":3,"pid":1,"name":"c"},
  62. * {"id":4,"pid":2,"name":"d"},{"id":5,"pid":4,"name":"e"},{"id":6,"pid":5,"name":"f"},
  63. * {"id":7,"pid":3,"name":"g"}]
  64. * 转换后:
  65. * [{"id":1,"pid":0,"name":"a","level":1},{"id":3,"pid":1,"name":"c","level":2},{"id":7,"pid":3,"name":"g","level":3},
  66. * {"id":2,"pid":0,"name":"b","level":1},{"id":4,"pid":2,"name":"d","level":2},{"id":5,"pid":4,"name":"e","level":3},
  67. * {"id":6,"pid":5,"name":"f","level":4}]
  68. * @author 段誉(2021/4/12 16:44)
  69. */
  70. public static function multilevel_linear_sort($data, $symbol = '', $name = 'name', $id_name = 'id', $parent_id_name = 'pid', $level = 1, $parent_id = 0)
  71. {
  72. $result = [];
  73. $this_symbol = '';
  74. for ($i = 0; $i < $level; $i++) {
  75. $this_symbol .= $symbol;
  76. }
  77. foreach ($data as $key => $row) {
  78. if ($row['pid'] == $parent_id) {
  79. $row['level'] = $level;
  80. $row[$name] = $this_symbol . $row['name'];
  81. $result[] = $row;
  82. $pid = $row['id'];
  83. unset($data[$key]);
  84. $child_data = self::multilevel_linear_sort($data, $symbol, $name, $id_name, $parent_id_name, ($level + 1), $pid);
  85. foreach ($child_data as $child_row) {
  86. $result[] = $child_row;
  87. }
  88. }
  89. }
  90. return $result;
  91. }
  92. /**
  93. * User: 意象信息科技 lr
  94. * Desc: 表单多维数据转换
  95. * 例:
  96. * 转换前:{"x":0,"a":[1,2,3],"b":[11,22,33],"c":[111,222,3333,444],"d":[1111,2222,3333]}
  97. * 转换为:[{"a":1,"b":11,"c":111,"d":1111},{"a":2,"b":22,"c":222,"d":2222},{"a":3,"b":33,"c":3333,"d":3333}]
  98. * @param $arr array 表单二维数组
  99. * @param $fill boolean fill为false,返回数据长度取最短,反之取最长,空值自动补充
  100. * @return array
  101. */
  102. public static function form_to_linear($arr, $fill = false)
  103. {
  104. $keys = [];
  105. $count = $fill ? 0 : PHP_INT_MAX;
  106. foreach ($arr as $k => $v) {
  107. if (is_array($v)) {
  108. $keys[] = $k;
  109. $count = $fill ? max($count, count($v)) : min($count, count($v));
  110. }
  111. }
  112. if (empty($keys)) {
  113. return [];
  114. }
  115. $data = [];
  116. for ($i = 0; $i < $count; $i++) {
  117. foreach ($keys as $v) {
  118. $data[$i][$v] = isset($arr[$v][$i]) ? $arr[$v][$i] : null;
  119. }
  120. }
  121. return $data;
  122. }
  123. }