将多维关联数组显示为表

时间:2016-01-18 11:27:14

标签: php arrays multidimensional-array

我有以下数组,我想在HTML TABLE FORMAT中显示它。例如:

<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>';

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

2 个答案:

答案 0 :(得分:1)

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

<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>

答案 1 :(得分:0)

这应该可行

<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>