如何在 PHP 中从数组创建嵌套列表(菜鸟)

发布于 2024-10-16 23:04:05 字数 1161 浏览 3 评论 0原文

我有一个应用程序,用户可以选择项目,然后填充一个包含两列的表格:类别和项目。它是这样的:

Category  | Items 
-----------------
PET       | Dog Food
HEALTH    | Tooth Brush
PET       | Cat Food
COLD FOOD | Steak
COLD FOOD | Eggs

在我的 select 语句中,我按类别对数据进行排序,然后按项目排序,并且我使用 WHILE 循环将数据放入数组中。

while($listItems = mysql_fetch_array($results))
{
    $myArray[] = array("category" => $listItems["Category"],
                       "item"     => $listItems["Name"]);
}

当我执行 print_r($myArray); 时我得到这样的结果:

Array ( [0] => Array ( [category] => Cold Food [item] => Steak )
        [1] => Array ( [category] => Food      [item] => Eggs )
        [2] => Array ( [category] => Health    [item] => Tooth Brush )
        [3] => Array ( [category] => Pet       [item] => Cat Food )
        [4] => Array ( [category] => Pet       [item] => Dog Food ) )

我想要做的是将数据输出到类似这样的嵌套列表中。

  • 冷食
    • 牛排
    • 鸡蛋
  • 保健
    • 牙刷
  • 宠物
    • 猫粮
    • 狗粮

欢迎任何帮助!谢谢!

I have an app that users choose items and then it fills a table that has two columns in it: Category and Items. Its something like this:

Category  | Items 
-----------------
PET       | Dog Food
HEALTH    | Tooth Brush
PET       | Cat Food
COLD FOOD | Steak
COLD FOOD | Eggs

In my select statement I order the data by Category then by Items and I am using a WHILE loop to drop the data into an array.

while($listItems = mysql_fetch_array($results))
{
    $myArray[] = array("category" => $listItems["Category"],
                       "item"     => $listItems["Name"]);
}

When I do a print_r($myArray); I get something like this:

Array ( [0] => Array ( [category] => Cold Food [item] => Steak )
        [1] => Array ( [category] => Food      [item] => Eggs )
        [2] => Array ( [category] => Health    [item] => Tooth Brush )
        [3] => Array ( [category] => Pet       [item] => Cat Food )
        [4] => Array ( [category] => Pet       [item] => Dog Food ) )

What I am wanting to do is output the data into a nested list something like this.

  • Cold Food
    • Steak
    • Eggs
  • Health
    • Tooth Brush
  • Pet
    • Cat Food
    • Dog Food

Any help is welcomed! Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

试试这个:

while($listItems = mysql_fetch_array($results))
{
    $cat  = $listItems['Category'];
    $name = $listItems['Name'];

    if(!isset($myArray[$cat]))
      $myArray[$cat] = array();

    $myArray[$cat][] = $name;
}

然后,要输出它,你可以这样做:

$buffer = '<ul>';
foreach($myArray as $cat => $items) {
   $buffer .= "<li>$cat</li><li><ul>";
   foreach($items as $item) {
      $buffer .= "<li>$item</li>";
   }
   $buffer .= '</ul></li>';
}
$buffer .= '</ul>';
echo $buffer;

Try this:

while($listItems = mysql_fetch_array($results))
{
    $cat  = $listItems['Category'];
    $name = $listItems['Name'];

    if(!isset($myArray[$cat]))
      $myArray[$cat] = array();

    $myArray[$cat][] = $name;
}

Then, to output it you'd do this:

$buffer = '<ul>';
foreach($myArray as $cat => $items) {
   $buffer .= "<li>$cat</li><li><ul>";
   foreach($items as $item) {
      $buffer .= "<li>$item</li>";
   }
   $buffer .= '</ul></li>';
}
$buffer .= '</ul>';
echo $buffer;
绝不放开 2024-10-23 23:04:05

尝试一下。其想法是将数据重新组织为更方便的形式,即按类别对项目建立索引。

代码

<?php
    $items = array
    (
        array('category' => 'Cold Food', 'item' => 'Steak'),
        array('category' => 'Food', 'item' => 'Eggs'),
        array('category' => 'Health', 'item' => 'Tooth Brush'),
        array('category' => 'Pet', 'item' => 'Cat Food'),
        array('category' => 'Pet', 'item' => 'Dog Food'),
    );

    // Group the items by category.
    $itemsByCategory = array();

    foreach ($items as $item)
    {
        $category = $item['category'];
        $name     = $item['item'];

        if (!array_key_exists($category, $itemsByCategory))
            $itemsByCategory[$category] = array();

        $itemsByCategory[$category][] = $name;
    }

    // Output nested lists for the items.
?>  <ul>
<?php foreach ($itemsByCategory as $category => $items)
      {
?>        <li><?php echo htmlspecialchars($category); ?>

              <ul>
<?php           foreach ($items as $item)
                {
?>                  <li><?php echo htmlspecialchars($item); ?></li>
<?php           }
?>            </ul>
          </li>
<?php }
?>  </ul>

输出

  • 冷食
    • 牛排
  • 食物
    • 鸡蛋
  • 健康
    • 牙刷
  • 宠物
    • 猫粮
    • 狗粮

Give this a try. The idea is to reorganize your data into a more convenient form, namely to have the items indexed by category.

Code

<?php
    $items = array
    (
        array('category' => 'Cold Food', 'item' => 'Steak'),
        array('category' => 'Food', 'item' => 'Eggs'),
        array('category' => 'Health', 'item' => 'Tooth Brush'),
        array('category' => 'Pet', 'item' => 'Cat Food'),
        array('category' => 'Pet', 'item' => 'Dog Food'),
    );

    // Group the items by category.
    $itemsByCategory = array();

    foreach ($items as $item)
    {
        $category = $item['category'];
        $name     = $item['item'];

        if (!array_key_exists($category, $itemsByCategory))
            $itemsByCategory[$category] = array();

        $itemsByCategory[$category][] = $name;
    }

    // Output nested lists for the items.
?>  <ul>
<?php foreach ($itemsByCategory as $category => $items)
      {
?>        <li><?php echo htmlspecialchars($category); ?>

              <ul>
<?php           foreach ($items as $item)
                {
?>                  <li><?php echo htmlspecialchars($item); ?></li>
<?php           }
?>            </ul>
          </li>
<?php }
?>  </ul>

Output

  • Cold Food
    • Steak
  • Food
    • Eggs
  • Health
    • Tooth Brush
  • Pet
    • Cat Food
    • Dog Food
余厌 2024-10-23 23:04:05
<?
//establish $vlink before
$query = 'select distinct (category) from list';
$result = mysql_query($query,$vlink);

while($row = mysql_fetch_array($results))
{
    $query1 = "select items from list where category = ".$row[0];
    $result1 = mysql_query($query1,$vlink);

    echo '<h1>'.$row[0].'</h1>';
    while($row1 = mysql_fetch_array($result1))
    {
        echo '<h2>'.$row1[0].'</h2>';
    }
}
?>
<?
//establish $vlink before
$query = 'select distinct (category) from list';
$result = mysql_query($query,$vlink);

while($row = mysql_fetch_array($results))
{
    $query1 = "select items from list where category = ".$row[0];
    $result1 = mysql_query($query1,$vlink);

    echo '<h1>'.$row[0].'</h1>';
    while($row1 = mysql_fetch_array($result1))
    {
        echo '<h2>'.$row1[0].'</h2>';
    }
}
?>
享受孤独 2024-10-23 23:04:05

尝试用这个:

while($listItems = mysql_fetch_array($results)) 
{
     $myArray[$listItems["Category"]][] = $listItems["Name"] 
}

Try with this:

while($listItems = mysql_fetch_array($results)) 
{
     $myArray[$listItems["Category"]][] = $listItems["Name"] 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文