我有下表:
============================================= Probe Name IP Address Credentials ============================================= NetApp 192.168.20.104 abc/abc123 --------------------------------------------- VMware 192.168.20.219 abc/abc123 --------------------------------------------- NetApp 192.168.20.143 admin/admin ---------------------------------------------
我想合并具有相同值的列并将其显示为一个,如下表所示。所以我想用HTML / PHP显示O / P,比如列"探测名称"数据" NetApp"合并为一行。
============================================= Probe Name IP Address Credentials ============================================= 192.168.20.104 abc/abc123 NetApp --------------------------------- 192.168.20.143 admin/admin --------------------------------------------- VMware 192.168.20.219 abc/abc123 ---------------------------------------------
public function runSql(){
$sql = "select probe_name from probe_info group by probe_name, ip_address, credential, prerequisite";
$stmt = $this->db->prepare($sql);
$stmt->execute();
if($stmt->rowCount() > 0){
$row = $stmt->fetch(PDO::FETCH_ASSOC) ;
return $row['probe_name'];
}
}
public function displayData($sql) {
try {
//echo "in displaydata fun";
$probe = crud::runSql();
$stmt = $this->db->prepare($sql);
$stmt->execute();
if($stmt->rowCount() > 0){
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
//print_r($row['credential']);
?>
<meta charset="UTF-8">
<tr>
<!-- td><?/*php if (isset($row['ID'])) { print_r($row['ID']); } */?></td -->
<!-- td><?php //if (isset($row['probe_name'])) { //echo "<strong>"; print ($row['probe_name']) ; echo "</strong>" ;} ?></td -->
<td><?php if (isset($row['probe_name'])) { echo $probe_name; } ?></td>
<td><?php if (isset($row['ip_address'])) { print_r($row['ip_address']); } ?></td>
<td><?php if (isset($row['credential'])) { print_r($row['credential']); } ?></td>
<td><?php if (isset($row['prerequisite'])) { print_r($row['prerequisite']); } ?></td>
<td><a href="" title="Edit probe info"><i class="glyphicon glyphicon-edit"></i></a></td>
<td><a href="" title="Delete probe record"><i class="glyphicon glyphicon-remove-circle"></i></a></td>
</tr>
<?php
}
}
} catch (Exception $ex) {
echo "Message -".$ex.getMessage();
}
}
答案 0 :(得分:0)
你可以&#39;分组&#39; PHP中的行,函数为array_reduce
,并使用了关联数组:
array_reduce manual,associative arrays manual
$groups = array_reduce($queryRows, function($groupArray, $row) {
if (!array_key_exists($row['Probe Name'], $groupArray) {
$groupArray[$row['Probe Name']] = [];
}
$groupArray[$row['Probe Name']][] = $row;
return $groupedArray;
}, []);
该函数迭代数组$queryRows
并在执行此操作时动态创建新的关联数组$groupArray
。在每次迭代中,如果$row['Probe name']
中的值已经是$groupedArray
中的一个键,则会进行检查。如果不是,则将其设置为新密钥。然后,该行将添加到关联数组中的该键。
这将创建以下数据结构:
$groups = [
'NetApp' => [
[
'Probe Name' => 'NetApp',
'IP Address' => '192.168.20.104',
'Credentials' => 'abc/abc123',
],
[
'Probe Name' => 'NetApp',
'IP Address' => '192.168.20.143',
'Credentials' => 'admin/admin',
]
],
'VMware' => [
[
'Probe Name' => 'VMware',
'IP Address' => '192.168.20.219',
'Credentials' => 'abc/abc123',
]
]
];
有关详细信息,请阅读提供的手册。