使用 PHP 和 MYSQL 构建多维关联数组

发布于 2024-09-17 12:14:39 字数 1990 浏览 2 评论 0原文

我正在尝试构建一个多维关联数组,同时从 MySQL 查询中获取结果。 但我不知道如何实现这一目标。 我想构建一个像这样的数组:

 array ("cat_name" => "category1",
 "id_cat" => "1",
 "sub_cat" => array ("name_sub_cat" => "subCategory",
"id_sub_cat" => "4",
       "ss_cat" =>array ("ss_cat_name" =>"ss_cat1",
    "id_ss_cat" => "4"
                           )
       )
  );

这是我构建数组的位置:
编辑:我最终得到了类似的东西,不是很性感,但如果我认为我就快到了

while($row = mysql_fetch_assoc($resultQueryCat)){
    $menuArray[$row["id_cat"]]["cat_name"] = $row["cat_name"];
    $menuArray[$row["id_cat"]][$row["id_sub_cat"]]["id_sub_cat"] = $row["id_sub_cat"];
    $menuArray[$row["id_cat"]][$row["id_sub_cat"]]["name_sub_cat"] = $row["name_sub_cat"];
    $menuArray[$row["id_cat"]][$row["id_sub_cat"]][$row["ss_cat_name"]]["ss_cat_name"] =     $row["ss_cat_name"];
    $menuArray[$row["id_cat"]][$row["id_sub_cat"]][$row["ss_cat_name"]]["id_ss_cat"] = $row["id_ss_cat"];
                                     }

编辑2:显示数组的代码

    $menu.='<ul>';
    foreach ($menuArray as $key) { 
       $compteur_cat++;
       $menu.= '<div id="collapsiblePanelCol'.$compteur_cat.'"    class="collapsiblePanelCol floatleft">
        <li class="categorie"> '.$key["cat_name"].'
           <ul>';
  foreach ($key as $key1) {
if (is_array($key1){/* One of the thing i forgot which totally screwed up my results*/
    $menu.= '<ul>
       <li class="ss_categorie">'.$key1["name_sub_cat"].'<ul>';
    foreach ($key1 as $key2) {
              if (is_array($key2)){ 
                   $menu.= '<li class="element">'.$key2["ss_cat_name"].'</li>'; }
                              }  
    $menu.= '</ul></li></ul>';
                   }
                }
$menu.='</ul>
   </li>
  </div>';
  } 
  $menu.= '</ul>';

谢谢。

最终编辑:我的代码正在运行:)我编辑了之前的代码以使其正确

I'm trying to build a multidimensional associative array while fetching the results from a MySQL query.
But i can't figure out how to achieve this.
I want to build an array like this one:

 array ("cat_name" => "category1",
 "id_cat" => "1",
 "sub_cat" => array ("name_sub_cat" => "subCategory",
"id_sub_cat" => "4",
       "ss_cat" =>array ("ss_cat_name" =>"ss_cat1",
    "id_ss_cat" => "4"
                           )
       )
  );

Here's where i'm building the array:
Edit : I've ended up with something like that, not very sexy, but if think i'm almost there

while($row = mysql_fetch_assoc($resultQueryCat)){
    $menuArray[$row["id_cat"]]["cat_name"] = $row["cat_name"];
    $menuArray[$row["id_cat"]][$row["id_sub_cat"]]["id_sub_cat"] = $row["id_sub_cat"];
    $menuArray[$row["id_cat"]][$row["id_sub_cat"]]["name_sub_cat"] = $row["name_sub_cat"];
    $menuArray[$row["id_cat"]][$row["id_sub_cat"]][$row["ss_cat_name"]]["ss_cat_name"] =     $row["ss_cat_name"];
    $menuArray[$row["id_cat"]][$row["id_sub_cat"]][$row["ss_cat_name"]]["id_ss_cat"] = $row["id_ss_cat"];
                                     }

Edit2: The code to display the array

    $menu.='<ul>';
    foreach ($menuArray as $key) { 
       $compteur_cat++;
       $menu.= '<div id="collapsiblePanelCol'.$compteur_cat.'"    class="collapsiblePanelCol floatleft">
        <li class="categorie"> '.$key["cat_name"].'
           <ul>';
  foreach ($key as $key1) {
if (is_array($key1){/* One of the thing i forgot which totally screwed up my results*/
    $menu.= '<ul>
       <li class="ss_categorie">'.$key1["name_sub_cat"].'<ul>';
    foreach ($key1 as $key2) {
              if (is_array($key2)){ 
                   $menu.= '<li class="element">'.$key2["ss_cat_name"].'</li>'; }
                              }  
    $menu.= '</ul></li></ul>';
                   }
                }
$menu.='</ul>
   </li>
  </div>';
  } 
  $menu.= '</ul>';

Thanks.

Final Edit: My code is working :) i edited the previous code to make it correct

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

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

发布评论

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

评论(2

挽心 2024-09-24 12:15:00

我构建多维数组的方式是通过类及其对象。

示例:

class Categories
{
    public $cat_name;
    public $id_cat;

    function __construct($cat_name, $id_cat)
    {
        $this->id = $id_cat;
        $this->name = $cat_name;
        $this->sub_cat = array();
    }       
}

class SubCategories
{
    public $name_sub_cat;
    public $id_sub_cat;

    function __construct($name_sub_cat, $id_sub_cat)
    {
        $this->id = $id_sub_cat;
        $this->name = $name_sub_cat;
        $this->ss_cat = array();
    }       
}

class SSCategories
{
    public $ss_cat_name;
    public $id_ss_cat;

    function __construct($ss_cat_name, $id_ss_cat)
    {
        $this->id = $id_ss_cat;
        $this->name = $ss_cat_name;
    }       
}

然后写入这些类(在获取 MySQL DB 数据之后 [假设 OOP mysql]):

if($result)
{
    $array = array();
    while($row = $result->fetch_array())
    {
        $array[] = new Categories($row["cat_name"], $row["id_cat"]);
    }

    foreach($array as $main)
    {
        ... (fetching sub category info)
        if($sub_result)
        {
            $sub_array = array();
            while($sub_row = $sub_result->fetch_array())
            {
                $sub_array[] = new SubCategories($sub_row["name_sub_cat"], $sub_row["id_sub_cat"]);
            }
            $main->sub_cat = $sub_array;

您可以对原始 if 循环中的每个类重复,以获得尽可能多的子数组。这花了一段时间:)。

注意:如果没有更多的子数组,则关闭 if 循环(按层次顺序)。

echo '<pre>';
print_r($array);
echo '</pre>';

上面的代码将有助于显示阵列的级别!

The way I build multidimensional arrays is through classes and their objects.

Example:

class Categories
{
    public $cat_name;
    public $id_cat;

    function __construct($cat_name, $id_cat)
    {
        $this->id = $id_cat;
        $this->name = $cat_name;
        $this->sub_cat = array();
    }       
}

class SubCategories
{
    public $name_sub_cat;
    public $id_sub_cat;

    function __construct($name_sub_cat, $id_sub_cat)
    {
        $this->id = $id_sub_cat;
        $this->name = $name_sub_cat;
        $this->ss_cat = array();
    }       
}

class SSCategories
{
    public $ss_cat_name;
    public $id_ss_cat;

    function __construct($ss_cat_name, $id_ss_cat)
    {
        $this->id = $id_ss_cat;
        $this->name = $ss_cat_name;
    }       
}

Then to write to those classes (after fetching MySQL DB Data [assuming OOP mysql]):

if($result)
{
    $array = array();
    while($row = $result->fetch_array())
    {
        $array[] = new Categories($row["cat_name"], $row["id_cat"]);
    }

    foreach($array as $main)
    {
        ... (fetching sub category info)
        if($sub_result)
        {
            $sub_array = array();
            while($sub_row = $sub_result->fetch_array())
            {
                $sub_array[] = new SubCategories($sub_row["name_sub_cat"], $sub_row["id_sub_cat"]);
            }
            $main->sub_cat = $sub_array;

You would repeat for each within original if loop for as many sub arrays as you have. This took a while :).

NOTE: Close if loops (heirarchally) if there are no more sub arrays.

echo '<pre>';
print_r($array);
echo '</pre>';

The above code will help show the levels of your array!

原谅我要高飞 2024-09-24 12:14:54

这不是一件容易的任务,因为数据库中的数据可能包含循环:-)

我最好将其保存在 id->data hashmap 中,以便您可以更轻松地构建结构。

不知道为什么你需要多维度。

It's not an easy task, as data from DB might contain loops :-)

I'd better save it in id->data hashmap so that you can build the structure easier.

Not sure why you need multidimensions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文