我需要通过字段dg_id组合数组的值并获得mark和cnt的结果。 dg_id字段值应该是唯一的。请参阅下面的代码
1.Array
Array (
[0] => Array
(
[dg_id] => ht01aa
[mark] => A
[cnt] => 24653
)
[1] => Array
(
[dg_id] => ht01aa
[mark] => O
[cnt] => 149
)
[2] => Array
(
[dg_id] => ht01aa
[mark] => U
[cnt] => 198
)
[3] => Array
(
[dg_id] => ht01ab
[mark] => A
[cnt] => 19259
)
[4] => Array
(
[dg_id] => ht01ab
[mark] => O
[cnt] => 121
)
[5] => Array
(
[dg_id] => ht01ab
[mark] => U
[cnt] => 153 )
)
)
如何以这种方式获得输出
+--------+--------+--------+-------+
| dg_id | A | O | U |
+--------+--------+--------+-------+
| ht01aa | 24653 | 149 | 198 |
| ht01ab | 19259 | 121 | 153 |
3.我已尝试过代码
$temp = '';
echo '<table cellpadding="10px">';
foreach ($status_array as $key1 => $val1) {
echo '<tr>';
foreach ($val1 as $key2 => $val2) {
if ($key2 == $status_array['dg_id'] && $temp!= $val2) {
$temp = $val2;
echo "<td><b>" . $val2 . "</b></td>";
echo '</tr>';
}
else if ($key2 == 'cnt']){
echo "<td>" . $val2 . "</td>";
}
}
echo '</tr>';
}
echo '</table>';
4.我当前的输出
dg_Id A O U
ht01aa 24653
149
198
ht01ab 19259
121
153
查询
$status="select concat(dg,fg,block) as dg_id, mark, count(*) as cnt from ".$table." group by 1,2 limit 15";
$status_fetch=mysql_query($status,$link1);
$status_array = array();
while ($row_status = mysql_fetch_array($status_fetch, MYSQL_ASSOC))
{
$status_array[] = $row_status;
}
但没有得到适当的解决方案。任何人帮我解决这个问题..
答案 0 :(得分:0)
从查询中获取数组值后,从for each循环开始
$result_array= Array();
foreach( $status_array as $fa )
{
$result_array[$fa["dg_id"]][($fa["mark"])] += $fa["cnt"];
}
制作以下内容
Array (
[ht01aa] => Array (
[A] => 24203
[O] => 323
[U] => 474
)
[ht01ab] => Array (
[A] => 18639
[O] => 392
[U] => 502
)
)
然后通过为每个循环使用另一个循环将数组值分配给表
foreach ($result_array as $dg_id => $result)
{
$keyA = "A";
$keyO = "O";
$keyU = "U";
echo "<tr>
<td>{$dg_id}</td>
<td>{$result[$keyA]}</td>
<td>{$result[$keyO]}</td>
<td>{$result[$keyU]}</td>
</tr>
}
这适合我:)