如何再次遍历数组结果,以便(按其中一个字段)将结果排列到PHP中的表中


How to go over an array result again in order to arrange (by one of the fields) the result into a table in PHP

我必须做一个按地点、供应商或资产描述排序的"详细报告"问题是,虽然我得到了正确的结果,但我想显示像

这样的结果位置1:

表,其中资产位于该位置

位置2:

提供者和描述也是一样…

我的PHP和其他语言知识有限。所以,我想做一些像……

获取数组(完整数组而不仅仅是一行),然后检查array[i]中的位置是否等于array[i+1],然后打印<td>与行数据,否则结束该表,然后创建一个新的

位置:

最后是另一个表,其中包含与该位置匹配的行…

如何在PHP中做到这一点?

例子:

这是我现在拥有的

这就是我想做的

这就是我在PHP中所做的

<?php
echo "<table>
        <thead>
            <tr>
                <th></th>
                <th>Ubicaci&oacute;n</th>
                <th>C&oacute;digo</th>
                <th>Descripci&oacute;n</th>
                <th>Costo Adquisici&oacute;n</th>
                <th>Saldo a Depreciar de Activos</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <tr>";
while($row = odbc_fetch_array($result)){
    echo"<td></td>
        <td><b>".$row["Ubicacion_actual"]."</b></td>
        <td>".$row["Activo"]."</td>
        <td>".$row["Descripcion"]."</td>
        <td>".number_format($row["Costo_Adquisicion"],4)."</td>
        <td>".number_format($row["Saldo_sin_depreciar"],4)."</td>
        <td></td>
        </tr>"; 
}
echo "</tbody>
    </table>";
odbc_close($dbhandle);
?>

首先,您应该将所有数据放入本地数组中,然后使用本地数组来处理它。

所以你可以写:

$theArray = array(); //Array to hold data
$headerIds = array(); //Array to hold unique id of the records
while($row = odbc_fetch_array($result)){
    array_push($theArray, $row);
    if(!in_array($row["Ubicacion_actual"], $headerIds))
    {
         array_push($headerIds, $row["Ubicacion_actual"]);
    }
}
odbc_close($dbhandle);
foreach($headerIds as $id)
{
    //Print the header info
    //...
    //Loop through each item in your data array until its id matches the header id
    foreach($theArray as $dataItem)
    {
        if($dataItem["Ubicacion_actual"] == $id) //This line item has the same id as the header
        {
            //Display the table row data
        }
    }
}