将多维关联数组显示为表


Displaying an Multidimensional associative array as a table

>我有以下数组,我想以HTML表格格式显示它。例如:

<table>
                            <tr>
                                <th>Case Number</td>
                                <th>Created On</td>
                                <th>Fault Type</td>
                                <th>Status</td>
                            </tr>
                            <tr>
                                <td id="fcase1">Case Number</td>
                                <td id="creat1">Created On</td>
                                <td id="ftype1">Fault Type</td>
                                <td id="stats1">Status</td>
                            </tr>

这是数组:

Array ( 
[GetRecentCasesResult] => Array ( 
    [CaseHistory] => Array ( 
        [0] => Array ( 
            [caseNumber] => CAS-00305-Q0S7Y5 
            [dateCreated] => 2016-01-17 11:00:09 AM 
            [faultType] => Repair Blocked Sewer - Small Pipes 
            [status] => Open 
        ) 
        [1] => Array ( 
            [caseNumber] => CAS-00308-W4F8F0 
            [dateCreated] => 2016-01-17 01:17:04 PM 
            [faultType] => Repair Pothole in Major Road 
            [status] => Open 
        ) 
        [2] => Array ( 
            [caseNumber] => CAS-00309-T8B2J6 
            [dateCreated] => 2016-01-17 01:44:38 PM 
            [faultType] => Repair Pothole in Major Road 
            [status] => Open 
        ) 
        [3] => Array ( 
            [caseNumber] => CAS-00311-K7H8D9 
            [dateCreated] => 2016-01-17 02:18:29 PM 
            [faultType] => Electricity outage - Business 
            [status] => Open 
        ) 
        [4] => Array ( 
            [caseNumber] => CAS-00303-C8M0K8 
            [dateCreated] => 2016-01-17 10:14:45 AM 
            [faultType] => Electricity outage - Business 
            [status] => Open 
        ) 
        [5] => Array ( 
            [caseNumber] => CAS-00306-R7W8S7 
            [dateCreated] => 2016-01-17 11:14:40 AM 
            [faultType] => Animal Carcass Removal 
            [status] => Open 
        ) 
        [6] => Array ( 
            [caseNumber] => CAS-00307-B5Q6C0 
            [dateCreated] => 2016-01-17 11:17:22 AM 
            [faultType] => Water Leak 
            [status] => Open
        ) 
    ) 
)

我试过这个:

echo '<table><tr>';
function process_array($array){
    foreach($array as $key => $value){
        if(is_array($value)){
            process_array($value);
        }else{
            echo '<td>'. $value . '</td>';
        }
    }
}
process_array($result);
echo '</tr></table>';

但我没有得到正确的结果。

您可以通过其索引以简单的方式对其进行操作:

<table>
    <tr>
        <th>Case Number</th>
        <th>Created On</th>
        <th>Fault Type</th>
        <th>Status</th>
    </tr>
    <?php
    foreach ($array['GetRecentCasesResult']['CaseHistory'] as $data) {
        ?>
        <tr>
            <td id="fcase1"><?php echo $data['caseNumber']; ?>Case Number</td>
            <td id="creat1"><?php echo $data['dateCreated']; ?>Created On</td>
            <td id="ftype1"><?php echo $data['faultType']; ?>Fault Type</td>
            <td id="stats1"><?php echo $data['status']; ?>Status</td>
        </tr>
    <?php
    }
    ?>
</table>

这应该有效

<table>
    <tr>
        <th>Case Number</td>
        <th>Created On</td>
        <th>Fault Type</td>
        <th>Status</td>
    </tr>
    <?php foreach ($array['GetRecentCasesResult']['CaseHistory'] as $key => $value) { ?>
        <tr>
            <td id="fcase<?php echo $key; ?>"><?php echo $value['caseNumber']; ?></td>
            <td id="creat<?php echo $key; ?>"><?php echo $value['dateCreated']; ?></td>
            <td id="ftype<?php echo $key; ?>"><?php echo $value['faultType']; ?></td>
            <td id="stats<?php echo $key; ?>"><?php echo $value['status']; ?></td>
        </tr>    
    <?php } ?>
</table>