无限级分类
引用
public function teamTree()
{
//模拟数据库数据
$list = array(
array('id' => 1, 'parent_id' => 0, 'name' => '河北省'),
array('id' => 2, 'parent_id' => 0, 'name' => '北京市'),
array('id' => 3, 'parent_id' => 1, 'name' => '邯郸市'),
array('id' => 4, 'parent_id' => 2, 'name' => '朝阳区'),
array('id' => 5, 'parent_id' => 2, 'name' => '通州区'),
array('id' => 6, 'parent_id' => 4, 'name' => '望京'),
array('id' => 7, 'parent_id' => 4, 'name' => '酒仙桥'),
array('id' => 8, 'parent_id' => 3, 'name' => '永年区'),
array('id' => 9, 'parent_id' => 1, 'name' => '武安市'),
);
//构造成以下数据结构
foreach ($list as $value) {
$itemNew[$value['id']] = $value;
}
// $array = array(
// 1 => array('id' => 1, 'parent_id' => 0, 'name' => '河北省'),
// 2 => array('id' => 2, 'parent_id' => 0, 'name' => '北京市'),
// 3 => array('id' => 3, 'parent_id' => 1, 'name' => '邯郸市'),
// 4 => array('id' => 4, 'parent_id' => 2, 'name' => '朝阳区'),
// 5 => array('id' => 5, 'parent_id' => 2, 'name' => '通州区'),
// 6 => array('id' => 6, 'parent_id' => 4, 'name' => '望京'),
// 7 => array('id' => 7, 'parent_id' => 4, 'name' => '酒仙桥'),
// 8 => array('id' => 8, 'parent_id' => 3, 'name' => '永年区'),
// 9 => array('id' => 9, 'parent_id' => 1, 'name' => '武安市'),
// );
//生成树
$rt_data = $this->getTree($itemNew);
return json_encode($rt_data);
}
public function getTree($items)
{
$tree = [];
foreach ($items as &$item) {
$item['title'] = $item['name'];
if (isset($items[$item['parent_id']])) {
$items[$item['parent_id']]['children'][] = &$items[$item['id']];
} else {
$tree[] = &$items[$item['id']];
}
}
return $tree;
}
/**
* 递归获取当前id下所有子id
*
* @param [type] $party_id
* @return void
*/
public function _getPartyIds($party_id)
{
$where['parent_id'] = $party_id;
$ids = '';
$list = db('party')->where($where)->field('id')->select();
if ($list) {
foreach ($list as $item) {
$ids .= ',' . $item['id'];
$ids .= $this->_getPartyIds($item['id']);
}
}
return $ids;
}
/**
* 检查是否超过指定长度
* 长度返回true
*
* @param [type] $str
* @param integer $length
* @return void
*/
function check_max_length($str, $length = 10)
{
$strLength = mb_strlen($str, "utf-8");
if ($strLength > $length) {
return true;
}
return false;
}
/**
* 判断是否是手机号
*
* @param [type] $str
* @param integer $length
* @return boolean
*/
function is_mobile($str)
{
if (preg_match("/^1[34578]\d{9}$/", $str)) {
return true;
} else {
return false;
}
}