Data.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace mall\utils;
  10. class Data {
  11. public static function checkTree($result, $data) {
  12. $tree = self::getTree($result, $data["pid"]);
  13. $flag = true;
  14. foreach ($tree as $v) {
  15. if ($v['pid'] == $data["id"]) {
  16. $flag = false;
  17. break;
  18. }
  19. }
  20. return $flag;
  21. }
  22. public static function getTree($data,$id=0) {
  23. $tree = [];
  24. while ($id > 0) {
  25. foreach ($data as $v) {
  26. if ($v['id'] == $id) {
  27. $tree[] = $v;
  28. $id = $v['pid'];
  29. break;
  30. }
  31. }
  32. }
  33. return $tree;
  34. }
  35. public static function familyProcess($data,$res=[],$pid='0',$level=1){
  36. foreach($data as $item){
  37. if($item['pid'] == $pid){
  38. $res[$item['id']] = $item;
  39. $res[$item['id']]['level'] = $item['pid'] == 0 ? '' : str_repeat('&nbsp;&nbsp;', $level*4) . ' ├ ';
  40. $res[$item['id']]['children'] = self::familyProcess($data,array(),$item['id'],$level+1);
  41. }
  42. }
  43. return $res;
  44. }
  45. public static function analysisTree($data,$res=[]){
  46. foreach($data as $item){
  47. $value = $item;
  48. unset($value["children"]);
  49. $res[] = $value;
  50. if(!empty($item["children"])){
  51. $res = array_merge($res,self::analysisTree($item["children"],[]));
  52. }
  53. }
  54. return $res;
  55. }
  56. }