使用PHP对数组进行排序以生成多级菜单列表 [英] Sort an array with PHP to generate a multilevel menu listing

查看:435
本文介绍了使用PHP对数组进行排序以生成多级菜单列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在PHP中生成一个多级菜单列表,但我还没有怎么做,我想通过简单的示例为我提供帮助,并使用其标题对Category和Sub category进行排序,如下所示:

I am trying to generate a list of multilevel menu in PHP but I have not got how to do it, I would like to help me with simple examples and sort Categories and Sub categories with their title, something like this:

<ul>
  <li>Category 1</li>
  <li>
    <h4>TITLE HERE</h4>
    <ul>
      <li>Category Child 1</li>
      <li>Category Child 2</li>
      <li>Category Child 3</li>
      <li>
        <h4>TITLE HERE</h4>
        <ul>
          <li>Category child 1</li>
          <li>Category child 2</li>
          <li>Category child 3</li>
        </ul>
      </li>
    </ul>
  </li>
  <li>Category 1</li>
  <li>Category 2</li>
  <li>Category 3</li>
</ul>

我在PHP中有一个数组:

$data = Array (
  '0' => Array (
      'id' => '1',
      'id_parent' => '',
      'name' => 'Sports'
  ),

  '1' => Array (
      'id' => '2',
      'id_parent' => '1',
      'name' => 'Football'
  ),

  '2' => Array (
      'id' => '3',
      'id_parent' => '1',
      'name' => 'Bascket'
  ),

  '3' => Array (
      'id' => '4',
      'id_parent' => '',
      'name' => 'Health'
  ),

  '4' => Array (
      'id' => '5', 
      'id_parent' => '4',
      'name' => 'Nutrition and diet'
  ),

  '5' => Array (
      'id' => '6',
      'id_parent' => '4',
      'name' => 'Beauty Salon'
  ),

  '6' => Array (
      'id' => '7',
      'id_parent' => '',
      'name' => 'Films'
  ),

  '7' => Array (
      'id' => '8',
      'id_parent' => '7',
      'name' => 'Armageddon'
  ),

  '8' => Array (
      'id' => '9',
      'id_parent' => '7',
      'name' => 'Apocalypse'
  ),

  '9' => Array (
      'id' => '10',
      'id_parent' => '',
      'name' => 'News'
  ),

  '10' => Array (
      'id' => '11',
      'id_parent' => '10',
      'name' => 'International'
  ),

  '11' => Array (
      'id' => '12',
      'id_parent' => '11',
      'name' => 'News from Syria'
  ),

  '12' => Array (
      'id' => '13',
      'id_parent' => '11',
      'name' => 'News from Palestine'
  )
);

我在MySQL中有一个表:

CREATE TABLE `categories` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `id_parent` mediumint(8) unsigned DEFAULT NULL,
  `name` varchar(100) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `FK_categories_UNIQUE` (`name`),
  KEY `FK_categories` (`id_parent`),
  CONSTRAINT `FK_categories` FOREIGN KEY (`id_parent`) REFERENCES `categories` (`id`) ON UPDATE CASCADE
)   ENGINE=InnoDB AUTO_INCREMENT=26 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records of categories
-- ----------------------------

INSERT INTO `categories` VALUES ('1', null, 'Sports');
INSERT INTO `categories` VALUES ('2', '1', 'Football');
INSERT INTO `categories` VALUES ('3', '1', 'Bascket');
INSERT INTO `categories` VALUES ('4', null, 'Health');
INSERT INTO `categories` VALUES ('5', '4', 'Nutrition and diet');
INSERT INTO `categories` VALUES ('6', '4', 'Beauty Salon');
INSERT INTO `categories` VALUES ('7', null, 'Films');
INSERT INTO `categories` VALUES ('8', '7', 'Armageddon');
INSERT INTO `categories` VALUES ('9', '7', 'Apocalypse');
INSERT INTO `categories` VALUES ('10', null, 'News');
INSERT INTO `categories` VALUES ('11', '10', 'International');
INSERT INTO `categories` VALUES ('12', '11', 'News from Syria');
INSERT INTO `categories` VALUES ('13', '11', 'News from Palestine');

推荐答案

我将为您提供帮助,但我想先说几句话;

I am going to help you, but I would love to to say couple of things first;

您的数据库设计不正确.如果您为每个元素都提供 parent_id ,那么您也应该为顶级父元素提供parent_id 0,或者在最佳情况下也应给null.这样,我们可以减少第一个foreach并自动获取主要类别.

Your database design is not proper. if you are giving parent_id for each element you should have given parent_id 0 or in best case null for top parent too. That way we could reduce first foreach and just fetch main categories automatically.

也许现在您有很多值,但是将来您应该考虑一下.考虑一下,如果您有成千上万的新闻,类别,并且每次必须遍历所有数据.

Maybe now you have very a few values but in future you should consider this. Think about that if you had thousands of news, categories and each time you have to loop through all the data.

反正这是您的代码

$parents = array();

foreach ($data as $key => $item) {
    if($item['id_parent'] === '') {
        $parents[$key] = $item;
    } 
}

?>

<ul>

<?php foreach ($parents as $parent) { ?>
     <li> <?php $parent['name'] ?> </li>
     <ul>
   <?php foreach ($data as $key => $item)  { ?>
        <?php if ($parent['id'] == $item['id_parent'])  { ?>
        <li> <?php $item['name'] ?> </li> 
        <?php } ?>
    <?php } ?>
    </ul> 
<?php } ?>

这就是结果

这篇关于使用PHP对数组进行排序以生成多级菜单列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆