Home >Backend Development >PHP Tutorial >PHP infinite classification (with layer depth) algorithm_PHP tutorial

PHP infinite classification (with layer depth) algorithm_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:11:321125browse

A PHP program that uses arrays to achieve infinite classification. Friends in need can refer to it. I won’t say much here and just copy the code.

The code is as follows
 代码如下 复制代码


$cates = array(
array(
'cid' => 1,
  'cname' => '新闻',
  'pid'   => 0
 ),

 array(
  'cid'   => 2,
  'cname' => '通知',
  'pid'   => 0
 ),

 array(
  'cid'   => 3,
  'cname' => '国内新闻',
  'pid'   => 1
 ),

 array(
  'cid'   => 4,
  'cname' => '国际新闻',
  'pid'   => 1
 ),

 array(
  'cid'   => 5,
  'cname' => '北京新闻',
  'pid'   => 3
 ),

 array(
  'cid'   => 6,
  'cname' => '上海新闻',
  'pid'   => 3
 ),

 array(
  'cid'   => 7,
  'cname' => '紧急通知',
  'pid'   => 2
 ),

 array(
  'cid'   => 8,
  'cname' => '一般通知',
  'pid'   => 2
 ),
);

/**
 * 生成菜单
 *
 * @param array $data 原始数据
 * @param integer $pid 当前分类的父id
 * @return array 处理后数据
 */
function createMenuTree($data = array(), $pid = 0)
{
 if (empty($data))
 {
  return array();
 }

 static $level = 0;

 $returnArray = array();

 foreach ($data as $node)
 {
  if ($node['pid'] == $pid)
  {
   $returnArray[] = array(
    'cid'   => $node['cid'],
    'cname' => $node['cname'],
    'level' => $level
   );

   if (hasChild($node['cid'], $data))
   {
    $level++;

    $returnArray = array_merge($returnArray, createMenuTree($data, $node['cid']));

    $level--;
   }
  }
 }

 return $returnArray;
}

/**
 * 检查是否有子分类
 *
 * @param integer $cid 当前分类的id
 * @param array $data 原始数据
 * @return boolean 是否有子分类
 */
function hasChild($cid, $data)
{
 $hasChild = false;

 foreach ($data as $node)
 {
  if ($node['pid'] == $cid)
  {
   $hasChild = true;
   break;
  }
 }

 return $hasChild;
}

header('Content-Type: text/html; charset=utf-8');

$result = createMenuTree($cates);

foreach ($result as $row)
{
 for ($i = 0; $i < $row['level']; $i++)
{
echo "t";
}

echo $row['cname'] . "n";
}
?>

Copy code

$cates = array(
array(
'cid' => 1,
'cname' => ' News',
'pid' => 0
),
array(
'cid' => 2,
'cname' => 'notification',
'pid' => 0
),

array(
'cid' => 3,
'cname' => ; 'Domestic News',
'pid' => 1
), array(
'cid' => 4,
'cname' => 'International News',
'pid' => 1
), array(
'cid' => 5,
' cname' => 'Beijing News',
'pid' => 3
), array(
'cid' => 6,
'cname' => 'Shanghai News',
'pid' => 3
), array(
'cid' => 7, 'cname' => 'Emergency Notification',
'pid' => 2
), array(
'cid' => 8 ,
'cname' => 'General Notice',
'pid' => 2
),
);/**
* Generate menu
*
* @param array $data original data
* @param integer $pid parent id of the current category
* @return array processed data
*/
function createMenuTree($data = array(), $pid = 0)
{
if (empty($data))
{
return array ();
}
static $level = 0;
$returnArray = array();
foreach ($data as $node)
{
if ($node['pid'] == $pid)
{
$returnArray[] = array(
'cid' => $node[' cid'],
'cname' => $node['cname'],
'level' => $level
); if (hasChild( $node['cid'], $data))
{
$level++; $returnArray = array_merge($returnArray, createMenuTree($data, $node['cid'] )); $level--;
}
}
} return $returnArray;
} /**
* Check if there are subcategories
*
* @param integer $cid id of the current category
* @param array $data original data
* @return boolean whether There are subcategories
*/
function hasChild($cid, $data)
{
$hasChild = false; foreach ($data as $node) {
if ($node['pid'] == $cid)
{
$hasChild = true;
break;
}
} return $hasChild;
}header('Content-Type: text/html; charset=utf-8');$result = createMenuTree($cates);foreach ($result as $row)
{
for ($i = 0; $i < $row['level']; $ i++)
{
echo "t";
}<🎜><🎜> echo $row['cname'] . "n";
}
? > http://www.bkjia.com/PHPjc/444626.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444626.htmlTechArticleA PHP program that uses arrays to achieve infinite classification. Friends in need can refer to it. I won’t go into details here. Just copy the code. The code is as follows Copy the code ?php $cates = ar...
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