PHP的新手并试图创建一个嵌套列表,可能是以草率的方式做,但考虑到特定的结构。我正在尝试检查列表结构是否与数据库值匹配。
问题:似乎无法迭代第三个foreach循环(引用数据库的循环)内的'buildValue'和'floorValue'变量(在foreach1& 2中创建)。也尝试while循环,结果相同。
是否有其他方法可以实现这一目标,或者我做错了什么/傻瓜?
$htmlArray = array();
$buildingArray = array();
$buildingArray["0"] = array("4","3","2","1","0","01","02");
$buildingArray["1"] = array("4","3","2","1","0","01","02");
$buildingArray["2"] = array("4","3","2","1","0","01","02");
foreach ($buildingArray as $bkey => $building)
{
switch ($bkey)
{
case "0":
$htmlArray[0]['building'] = "Biology";
break;
case "1":
$htmlArray[1]['building'] = "Chemistry";
break;
case "2":
$htmlArray[2]['building'] = "Environmental";
break;
}
$buildVal = $bkey;
foreach ($building as $fkey => $floor)
{
$floorVal = $floor;
$prepend ='Floor ';
$title = $prepend.$floor;
$htmlArray[$fkey]['floor'] = $title;
// Iterate over the items results
//NO PROBLEM: These values (reference foreach1 & foreach2) output correct result!
echo("build=".$buildVal." ");
echo("floor=".$floorVal." ");
$roomArray= array();
$roomArray = $rooms -> fetchAll(PDO::FETCH_ASSOC);
foreach ($roomArray as $row)
{
//PROBLEM: These values (reference foreach1 & foreach2) do not iterate inside foreach 3, only output 1st building and floor!
echo("build=".$buildVal." ");
echo("floor=".$floorVal." ");
//echo any results matching building and floor
if($row["building"] == $buildVal+3 && $row["floor"] == $floorVal)
{
// echo($row["room");
}
}
}
}
没有问题输出: build = 0 floor = 4 build = 0 floor = 3 build = 0 floor = 2 build = 0 floor = 1 build = 0 floor = 0 build = 0 floor = 01 build = 0 floor = 02 build = 1 floor = 4 build = 1楼= 3楼= 1楼= 2楼= 1楼= 1楼= 1楼= 0楼= 1楼= 01楼= 1楼= 02楼= 2楼= 4楼= 2楼= 3楼= 2楼= 2 build = 2 floor = 1 build = 2 floor = 0 build = 2 floor = 01 build = 2 floor = 02
问题输出: build = 0 floor = 4 build = 0 floor = 4 build = 0 floor = 4 build = 0 floor = 4 build = 0 floor = 4 build = 0 floor = 4 build = 0 floor = 4 build = 0 floor = 4
$ RoomArray输出(除了最后的空数组似乎正确): 数组([0] =>数组([room_id] => 1 [房间] => 112 [地板] => 1 [建筑物] => 3)[1] =>数组([room_id] => 2 [房间] => 111 [楼层] => 1 [建筑物] => 3)[2] =>数组([room_id] => 3 [房间] => 116 [楼层] => 1 [建筑物] => 4)[3] =>数组([room_id] => 4 [房间] => 115 [楼层] => 1 [建筑物] => 4) [4] =>数组([room_id] => 5 [房间] => 114 [floor] => 1 [建筑物] => 4)[5] =>数组([room_id] => ; 6 [room] => 113 [floor] => 1 [building] => 4)[6] =>数组([room_id] => 7 [room] => 110 [floor] = > 1 [建筑物] => 5)[7] =>数组([room_id] => 8 [房间] => 109 [楼层] => 1 [建筑物] => 5))数组()Array()Array()Array()Array()Array()Array()Array()Array()Array()Array()Array()Array()Array()Array()Array()Array() Array()Array()Array()
也许我应该重新解释一下这个问题....上面是我的数据库(尽管数据会大得多),除了有3栋楼和6层楼,我需要一个如下的嵌套列表:
-Building1
-Floor1
-Room121
-Building2
-Floor1
-Floor2
-Room221
等
答案 0 :(得分:0)
我可以看到索引$ htmlArray变量的一些问题,但在$ roomArray进入场景后我不确定你在做什么。 看看下面的代码,也许有帮助。如果不是 - 确切地告诉$ roomArray循环有什么问题,什么是$ roomArray。
编辑我更新了我的答案,检查这是否为您提供所需的输出。
$buildingArray["0"] = array("4","3","2","1","0","01","02");
$buildingArray["1"] = array("4","3","2","1","0","01","02");
$buildingArray["2"] = array("4","3","2","1","0","01","02");
# Added simple dictionary
$btype = array("Biology","Chemistry","Environmental");
# Prefetch all rooms.
$roomArray= array();
$roomArray = $rooms -> fetchAll(PDO::FETCH_ASSOC);
#
foreach ($buildingArray as $bkey => $building)
{
# This is much nicer than case...
$htmlArray[$bkey]['building'] = $btype[$bkey];
foreach ($building as $fkey => $floor)
{
# Two things here - first, user sprintf instead of '.'.
# Second - You got keys owerwritten with using $fkey as the first key.
$htmlArray[$bkey]['floor'][$fkey]['title'] = sprintf('Floor %s',$floor);
foreach ($roomArray as $room)
{
if($room['building']==($bkey+3) and $room['floor']==$fkey)
{
$htmlArray[$bkey]['floor'][$fkey]['rooms'][$room['room_id']] = $room['room'];
}
}
}
}
echo '<pre>';
print_r($htmlArray);
echo '</pre>';