数据库结果到二级菜单

Database results to two level menu

这是我从数据库中得到的结果 (mysql)。现在我想获得结构类似于

的菜单
department title1
- project1
- project2
department title2
- project3
- project4
....

这是我的结果

project title   |   department title    | department_id 
 project1       |   department1         |   9
 project2       |   department2         |   2
 project3       |   department3         |   1
 project11      |   department1         |   9
 project23      |   department2         |   2
 project24      |   department3         |   1
 project15      |   department1         |   9
 project26      |   department2         |   2
 project21      |   department4         |   4

我正在使用 laravel 4.2。(这将采用 blade 语法)如果有帮助的话。

这是我到目前为止尝试过的: 在我的控制器中

//projects is above results from db query builder
 foreach($projects as $project)
         {
            if($project->department_id != "")
            {
              $department = array(
               "id" => $project->department_id,
               "title" => $project->department_title, 
              );
              $departments_projects[] = array_push($departments_projects,$department);
            }
         }

然后在我看来(我正在构建菜单的地方)我有这段代码

 <ul class="sub-menu">
            @foreach($departments as $department)
            <li class="">
              <a href="javascript:;">
                {{ $department['title'] }} <i class="icon-arrow"></i>
              </a>
              <ul class="sub-menu">
                @foreach($projects as $project)
                  @if($project->department_id == $department['id'] && $project->title != "")
                    <li>
                      <a href="{{ route('department.project.show',array($project->department_id,$project->id)) }}">
                        {{ $project->title }}
                      </a>
                    </li>
                  @endif
                @endforeach

              </ul>
            </li>
            @endforeach
          </ul>

也许在 MySQL 结束时解决这个问题会更好,但如果你想在检索结果后解决这个问题,你可以尝试以下方法:

该解决方案的前提是您具有以下结构的结果:

$projects = [
    [
        'project_title' => 'project2',
        'department_title' => 'department2',
        'department_id' => 2
    ],
    [
        'project_title' => 'project1',
        'department_title' => 'department1',
        'department_id' => 9
    ],
    [
        'project_title' => 'project3',
        'department_title' => 'department3',
        'department_id' => 1
    ],
    [
        'project_title' => 'project11',
        'department_title' => 'department1',
        'department_id' => 9
    ],
    [
        'project_title' => 'project13',
        'department_title' => 'department2',
        'department_id' => 2
    ],
];

我们最初得到一个独特的部门标题数组,我们按字母顺序对其进行排序。然后,我们创建一个 $departments 数组,其中包含部门标题作为键并包含我们将使用的结构。

然后,我们遍历 $projects 数组,并在 $departments 数组的适当字段中填写详细信息。

最后,我们也按字母顺序对项目进行排序。

$department_names = array_unique(array_column($projects, 'department_title'));
sort($department_names);

$departments = array_fill_keys($department_names, [
    'id' => null,
    'projects' => []
]);

foreach($projects as $project) {
    $departments[$project['department_title']]['id'] = $project['department_id'];
    $departments[$project['department_title']]['projects'][] = $project['project_title'];
}

foreach($departments as $key => $department) {
    sort($departments[$key]['projects']);
}

var_dump($departments);

这将产生:

array(3) {
  ["department1"]=>
  array(2) {
    ["id"]=>
    int(9)
    ["projects"]=>
    array(2) {
      [0]=>
      string(8) "project1"
      [1]=>
      string(9) "project11"
    }
  }
  ["department2"]=>
  array(2) {
    ["id"]=>
    int(2)
    ["projects"]=>
    array(2) {
      [0]=>
      string(9) "project13"
      [1]=>
      string(8) "project2"
    }
  }
  ["department3"]=>
  array(2) {
    ["id"]=>
    int(1)
    ["projects"]=>
    array(1) {
      [0]=>
      string(8) "project3"
    }
  }
}

您可以使用 $departments 数组生成类似于以下内容的内容:

<ul>
<?php foreach($departments as $key => $department) { ?>
    <li>
        <?php echo $key; ?>
        <ul>
            <?php foreach($department['projects'] as $project) { ?>
            <li><?php echo $project; ?></li>
            <?php } ?>
        </ul>
    </li>
<?php } ?>
</ul>

另一种方法是使用部门 ID 作为键,然后根据标题值按字母顺序对数组进行排序。

$department_ids = array_unique(array_column($projects, 'department_id'));

$departments = array_fill_keys($department_ids, [
    'title' => null,
    'projects' => []
]);

foreach($projects as $project) {
    $departments[$project['department_id']]['title'] = $project['department_title'];
    $departments[$project['department_id']]['projects'][] = $project['project_title'];
}

foreach($departments as $key => $department) {
    sort($departments[$key]['projects']);
}

uasort($departments, function ($x, $y) {
    return strcasecmp($x['title'], $y['title']);
});

var_dump($departments);

这将产生:

array(3) {
  [9]=>
  array(2) {
    ["title"]=>
    string(11) "department1"
    ["projects"]=>
    array(2) {
      [0]=>
      string(8) "project1"
      [1]=>
      string(9) "project11"
    }
  }
  [2]=>
  array(2) {
    ["title"]=>
    string(11) "department2"
    ["projects"]=>
    array(2) {
      [0]=>
      string(9) "project13"
      [1]=>
      string(8) "project2"
    }
  }
  [1]=>
  array(2) {
    ["title"]=>
    string(11) "department3"
    ["projects"]=>
    array(1) {
      [0]=>
      string(8) "project3"
    }
  }
}