0

我有两个具有相同数据但只有一个单元格与其他单元格不同的行,如下所示:-

Customer Name   |   No Room   |   Room Type
 Maikle dived         2             single
 Maikle dived         1             double

所以我不会像这样将客户合并到一个单元格上:-

Customer Name   |   No Room   |   Room Type
                      2             single
  Maikle dived            
                      1             double

我如何通过 PHP 和 mysql 做到这一点

4

1 回答 1

0

如果您尝试将其输出为 HTML,请执行以下操作:

 //$source should hold the data from DB
 $source = array(
   array('Maikle dived' => array('No Room' => 2, 'Room Type' => 'single'),
   array('Maikle dived' => array('No Room' => 1, 'Room Type' => 'double'),
 );

$result = array();

foreach($source as $item) {
    $user = key($item);
    $val  = current($item);

    if(!isset($result[$user])) {
        $result[$val] = array();
    }
    $result[$user][] = $val;
}

然后你可以循环通过 $result 来构建 html

foreach($result as $user => $vals) {
    echo $user;
    echo '<ul>';
    foreach($vals as $val) {
        echo '<li>'.$val.'</li>';
    }
    echo '</ul>';
}
于 2013-03-25T18:47:17.477 回答