Home  >  Article  >  Backend Development  >  Unlimited categories, query data from database, recursively perform custom sorting in arrays

Unlimited categories, query data from database, recursively perform custom sorting in arrays

WBOY
WBOYOriginal
2016-07-25 09:02:08871browse
Unlimited classification, querying data from the database, and recursively performing custom sorting in the array. I also realized that there are many shortcomings and they are not handled well. There may be a better way. Let's discuss it together. It's more than 100 lines. Not all
  1. class Tree{
  2. protected $arr = array(); //Sort array
  3. protected $info = array(); //Storage error information
  4. protected $tree = array(); //Storage generated class array
  5. protected $flag = true; //Flag bit
  6. public function __construct()
  7. {
  8. }
  9. //Get data
  10. public function data($fid,$arr)
  11. {
  12. $this->checkArr($arr );
  13. $this->checking();
  14. $this->chindAll($fid);
  15. return $this->tree;
  16. }
  17. //Judge whether the parameters meet the requirements
  18. protected function checkArr($ val)
  19. {
  20. static $num = 0;
  21. if(is_array($val)){
  22. foreach($val as $Varr){
  23. $this->isKeyVal('id', $Varr, $num);
  24. $this->isKeyVal('parentid', $Varr, $num);
  25. $this->isKeyVal('name', $Varr, $num);
  26. $num++;
  27. }
  28. }
  29. $this- >arr = $val;
  30. }
  31. //Whether the stored id is an array
  32. protected function checkNum($val)
  33. {
  34. if(!is_numeric($val)){
  35. $this->info[] = ' The incoming parameter '.$val.' is not a numerical value';
  36. $this->flag = false;
  37. $this->checking();
  38. }
  39. }
  40. //Verification, output error message
  41. protected function checking( ){
  42. if(!$this->flag){
  43. echo '
    ';
  44. var_dump($this->info);
  45. exit();
  46. }
  47. }
  48. //Judge array key Whether it exists and whether it has a value
  49. protected function isKeyVal($key, $arr, $num)
  50. {
  51. if(!array_key_exists($key, $arr))
  52. {
  53. $this->info[] = $num. 'Array key'.$key.'does not exist';
  54. $this->flag = false;
  55. }
  56. }
  57. //Get the son
  58. public function getChind($fid)
  59. {
  60. static $num = 0 ;
  61. $arr = array();
  62. $this->checkNum($fid);
  63. foreach($this->arr as $key=>$row){
  64. if( $row['parentid'] == $fid){
  65. $arr[] = $row;
  66. unset($this->arr[$key]);
  67. }
  68. }
  69. if(!empty($arr)){
  70. $num++;
  71. return $this->sortArr($arr);
  72. }else{
  73. return null;
  74. }
  75. }
  76. //Get my son and grandson
  77. public function chindAll($fid,$input=null)
  78. {
  79. static $ n =0;
  80. $n++;
  81. $arr = $this->getChind($fid);
  82. if(!empty($arr)){
  83. $count = count($arr);
  84. if(empty($ input)){
  85. for($i=0; $i<$count ;$i++){
  86. $this->tree[$i] = $arr[$i];
  87. }
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn