使用 PHP 和 MYSQL 构建多维关联数组
我正在尝试构建一个多维关联数组,同时从 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我构建多维数组的方式是通过类及其对象。
示例:
然后写入这些类(在获取 MySQL DB 数据之后 [假设 OOP mysql]):
您可以对原始 if 循环中的每个类重复,以获得尽可能多的子数组。这花了一段时间:)。
注意:如果没有更多的子数组,则关闭 if 循环(按层次顺序)。
上面的代码将有助于显示阵列的级别!
The way I build multidimensional arrays is through classes and their objects.
Example:
Then to write to those classes (after fetching MySQL DB Data [assuming OOP mysql]):
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.
The above code will help show the levels of your array!
这不是一件容易的任务,因为数据库中的数据可能包含循环:-)
我最好将其保存在 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.