使用带有多维数组的foreach()

时间:2014-02-08 17:26:25

标签: php mysql arrays multidimensional-array foreach

我有一个mySQL表,可以简化为这样:

animal      breed
Cat         Persian
Cat         Siamese
Dog         Alsatian
Dog         Poodle

我想以这种形式显示信息:

<table>
<tr>
<td class="heading">Cat</td>
</tr>
<tr>
<td class="body">Persian</td>
<td class="body">Siamese</td>
</tr>
<tr>
<td class="heading">Dog</td>
</tr>
<tr>
<td class="body">Alsatian</td>
<td class="body">Poodle</td>
</tr>
</table>

到目前为止,我有一个这样的查询:

$query = "SELECT * FROM aa_animal";
$result = mysql_query($query, $MySQL_extranet) or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
    $resultarray[] = $row;
}

然后我想我需要一对嵌套的foreach()循环,但它是多维数组的处理让我绝望地失去了。

foreach("unique_animal") {
  <tr>
    <td class-"heading">$animal</td>
  </tr>
  if ($animal=="unique_animal") {
    foreach("row") {
    <tr>
      <td class-"body">$breed</td>
    </tr>
  }
}

如何操作$ resultarray以获得$ animal,$ breed等的正确值?

进展如此

Justin的回复提供了上述问题的解决方案。但是,真实页面在数据库表中有更多列。这是我最初提供的简单示例和我尝试创建的页面之间的中间文件的代码。

<?php

$countryid='spain';

// MySQL query to select hotels and display all the hotel info
$query = "SELECT hid, hotel, pricefrom, place, region, country, picture, property, prop2, rooms, summary, lat, lng, zoomloc, breakfast, searchparam, specialoffer, biz_model, ta_rating, bc_rating FROM ex_hotel WHERE country LIKE '%$countryid%' AND showhide = 'show' ORDER BY orderr DESC";
$result = mysql_query($query, $MySQL_extranet) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
   if (isset($resultarray[$row['region']])) {
      $resultarray[$row['region']][] = $row['hotel'];
   }else{
      $resultarray[$row['region']] = array($row['hotel']);
   }
}
ksort($resultarray);
?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>

<body>
<table width="500" border="1" cellspacing=" 4" cellpadding="4">
<?php foreach($resultarray as $region => $hotel)
{
   echo '<tr><td colspan="2" style="background-color:#ddd">'.$region.'</td></tr>';

   foreach($hotel as $value) {
      echo '<tr><td width="250px">'.$value.'</td>';
   }
   foreach($hid as $value2) {
      echo '<td>'.$value2.'</td></tr>';
   }
}?>
</table>
</body>
</html>

mySQL查询中的所有值都需要在表中使用,但我该如何获取它们?

1 个答案:

答案 0 :(得分:2)

您应该在抓取时对数据进行分组

while($row = mysql_fetch_assoc($result))
{
   if (isset($resultarray[$row['animal']]))
      $resultarray[$row['animal']][] = $row['breed'];
   else
      $resultarray[$row['animal']] = array($row['breed']);
}

然后,显示它

foreach($resultarray as $animal => $breed)
{
   echo '<tr><td class="heading">'.$animal.'</td></tr>';

   foreach($breed as $value)
      echo '<tr><td class="body">'.$value.'</td></tr>';
}

编辑: 如果您需要更多列,您需要做的是存储完整行而不是一个项目:

while($row = mysql_fetch_assoc($result))
{
   if (isset($resultarray[$row['region']]))
      $resultarray[$row['region']][] = $row;
   else
      $resultarray[$row['region']] = array($row);
}

然后,您还可以获取这些行

foreach($resultarray as $region => $rows)
{
   echo '<tr><td class="heading">'.$region.'</td></tr>';

   // this way (display everything)
   foreach($rows as $row)
      foreach($row as $key => $value)
         echo '<tr><td class="body">'.$key.': '.$value.'</td></tr>';

   // or this way (if you want to display only specific item)
   foreach($rows as $row)
      echo '<tr><td class="body">Hotel: '.$row["hotel"].' / price: '.$row["pricefrom"].'</td></tr>';
}