基于不同键的回显数组

Echo array based on distinct key

我已经“排序”了一个数据库,按国家/地区升序排列,然后是年份降序排列。每个数据库记录包含:国家名称、年份、详细信息。有很多重复的国家,但年份不同。例如:

Albania,  2000, details  
Albania,  1965, details  
Croatia, 2014, details  
Croatia, 2003, details

无法弄清楚如何回显数组以获得如下所示的结果,其中国家/地区位于一行,年份和详细信息在下面列出,而没有重复国家/地区的名称:

Albania  
    2000,  details  
    1965,  details  

Croatia  
    2014,  details  
    2003,  details  

似乎我需要每个不同的国家、年份和详细信息?

这是我的 php 到目前为止:

$result = mysql_query("SELECT country, year, details FROM studies ORDER BY country, year DESC ");  
    //output data from each row in db  
    while($row = mysql_fetch_array($result))  {  
echo "   Country:  " .$row['country']. "<br />      Year:  " .$row['year'].  "    Details:  ".$row['details']. "<br /><br /> ";  
}  

非常感谢任何帮助,我很困惑!

尝试添加国家检查:

$newcountry = '';
 while($row = mysql_fetch_array($result))  {
   if ($newcountry != $row['country']) {
     echo "Country:". $row['country']."<br />";
     $newcountry = $row['country'];
   }
  echo " Year:  " .$row['year'].  "    Details:  ".$row['details']. "<br /><br /> ";
}

这应该有效,因为您已按国家/地区对查询进行排序。这很关键,否则您绝对应该在 SQL.

中添加 GROUP BY 子句

编辑: 在组周围添加一个 <div>,您只需更改回显序列,首先检查国家是否已经设置一次。它看起来像:

 $newcountry = 'undefined';
     while($row = mysql_fetch_array($result))  {
       if ($newcountry !='undefined' && $newcountry != $row['country']){
         echo '</div>'; // only add a closing div if a new country (and not 1st)
       }
       if ($newcountry != $row['country']) {
         echo "Country:". $row['country']."<br /><div class='countryDetail'>";
         $newcountry = $row['country'];
       }// added the start of the <div>
      echo " Year:  " .$row['year'].  "    Details:  ".$row['details']. "<br /><br /> ";
    }
    if ($newcountry != 'undefined') { //make sure at least one <div> is set
      echo "</div>"; // close the last <div>
    }

我将 class countryDetail 添加到 div,因此您可以在 jQuery 中将其与 toggle 一起使用。

您可以使用嵌套的 while 循环。您可能还想使用 PDO/mysqli_ functions/prepared 语句代替 mysql_ 函数:

// get unique country list first
$sql1 = "SELECT DISTINCT(country) FROM studies ORDER BY country";
$result1 = mysql_query($sql1);

// iterate through result set of sql1
while($row1 = mysql_fetch_array($result1))   
{
     $country = $row1['country'];
     echo "<br>";      // new line
     echo $country;      

     // get year, details for each country
     $sql2 = "SELECT year, details FROM studies WHERE country = '$country' ORDER BY year DESC";
     $result2 = mysql_query($sql2);
     // iterate through result set of $sql2 
     while ($row2 = mysql_fetch_array($result2))
     {
          echo "<br>";      // new line
          echo $row2['year']. ", " . $row2['details']; 
     }
}

循环派对!

$rows = [];
while($row = mysql_fetch_array($result))  {
    $rows[ $row['country'] ] = [ $row['year'], $row['details'] ];
}

foreach($rows as $country => $row) {
  $details = array_map(function($items) { return sprintf("\t - %s: %s\n", $items[0], $items[1]) }, $row);
  echo sprintf("\n%s:\n %s", $country, $details);
}