实现无限极分类的一个类、以及统计每个等级有多少的个数(存在父子级关系的字段)

 function get_array($ids =' ' )

             {

                     $sql = "select * from  " . tablename('表明') . " WHERE upopenid = :upopenid";

                      $items = pdo_fetchall($sql,array('upopenid' => $ids));

                     //判断一下你的数组是否为空

                    if(!empty($items))
                        {
                            foreach ($items as $key => $value)
                            {

                                //命名一个新的数组

                                  $son = [];

                                if($value['upopenid'] == $ids)
                                {

                                    $son = get_array($value['id']);
                                    $items[$key]['son'] = $son;
                                }

                            }
                                                     
                        }
                            return $items;
                            // return $lev;
                       }
                       $items = get_array($id);

             }

       //接下来就是统计一个多维数组键值的个数   和每个等级的个数

     function get_num($items)
                       {
                            static $lev=array();

                            foreach ($items as $k => $v) {

                                $lev[$v['id']]=$v['a']; //这里的a可以是你想要根据什么统计个数的字段(年龄、身份等级等)

                                if(!empty($v['son']))get_num($v['son']);

                            }

                            return $lev;

                        }

                        $num= get_num($items);


                        $zong = count($num);


                        $ge = array_count_values ($num);

你可能感兴趣的:(php)