如何在PHP中的两个不同的html表中打印多维数组?

时间:2015-04-11 10:44:22

标签: php arrays multidimensional-array foreach html-table

这是我的数组:

Array
(
    [status] => OK
    [result] => Array
        (
            [pnr] => 4106121148
            [cls] => SL
            [eticket] => 1
            [journey] => 17-Apr-2015
            [trainno] => 16629
            [name] => MALABAR EXP
            [from] => CNGR
            [to] => MAQ
            [brdg] => CNGR
            [passengers] => Array
                (
                    [0] => Array
                        (
                            [bookingstatus] => S10  41GN
                            [currentstatus] => CNF
                            [coach] => 
                        )

                    [1] => Array
                        (
                            [bookingstatus] => S10  42GN
                            [currentstatus] => CNF
                            [coach] => 
                        )

                    [2] => Array
                        (
                            [bookingstatus] => S10  43GN
                            [currentstatus] => CNF
                            [coach] => 
                        )

                )

            [chart] => CHART NOT PREPARED
            [error] => 
        )

)

我尝试将其打印到两个不同的html表tr。

if($responseArray['status'] === 'OK') {

        $pnr= '<tr>';
        $pnr.= '<td>'.$responseArray['result']['pnr'].'</td>';
        $pnr.= '<td>'.$responseArray['result']['journey'].'</td>';
        $pnr.= '<td>'.$responseArray['result']['name'].'</td>';
        $pnr.= '<td>'.$responseArray['result']['trainno'].'</td>';
        $pnr.= '<td>'.$responseArray['result']['cls'].'</td>';
        $pnr.= '<td>'.$responseArray['result']['from'].'</td>';
        $pnr.= '<td>'.$responseArray['result']['brdg'].'</td>';
        $pnr.= '<td>'.$responseArray['result']['to'].'</td>';
        $pnr.= '<td>'.$responseArray['result']['chart'].'</td>';
        $pnr.= '</tr>';

       $i=0;
       foreach($responseArray['result']['passengers'] as $passengers){
            $i++;
            $psngr='<tr>';
            $psngr.= '<td>Pasenger No '.$i;.'</td>';
            $psngr.= '<td>'.$passengers['bookingstatus'].'</td>';
            $psngr.= '<td>'.$passengers['currentstatus'].'</td>';
            $psngr.='</tr>';
        }

        echo $pnr.'#'.$psngr;
}

我连接两个表tr字符串并打印。第一个字符串($pnr)打印正常。在第二个字符串($psngr)中,我有三个值,但只打印最后一个值,即

<tr><td>Pasenger No 3</td><td>S10  43GN</td><td>CNF</td></tr>

应该是

<tr><td>Pasenger No 1</td><td>S10  41GN</td><td>CNF</td></tr>
<tr><td>Pasenger No 2</td><td>S10  42GN</td><td>CNF</td></tr>
<tr><td>Pasenger No 3</td><td>S10  43GN</td><td>CNF</td></tr>

2 个答案:

答案 0 :(得分:0)

每次迭代都会覆盖表行变量。将代码更改为以下代码应该有效(请注意foreach部分):

if($responseArray['status'] === 'OK') {

    $pnr= '<tr>';
    $pnr.= '<td>'.$responseArray['result']['pnr'].'</td>';
    $pnr.= '<td>'.$responseArray['result']['journey'].'</td>';
    $pnr.= '<td>'.$responseArray['result']['name'].'</td>';
    $pnr.= '<td>'.$responseArray['result']['trainno'].'</td>';
    $pnr.= '<td>'.$responseArray['result']['cls'].'</td>';
    $pnr.= '<td>'.$responseArray['result']['from'].'</td>';
    $pnr.= '<td>'.$responseArray['result']['brdg'].'</td>';
    $pnr.= '<td>'.$responseArray['result']['to'].'</td>';
    $pnr.= '<td>'.$responseArray['result']['chart'].'</td>';
    $pnr.= '</tr>';

   $i=0;
   $psngr = '';
   foreach($responseArray['result']['passengers'] as $passengers){
        $i++;
        $psngr.='<tr>';
        $psngr.= '<td>Pasenger No '.$i;.'</td>';
        $psngr.= '<td>'.$passengers['bookingstatus'].'</td>';
        $psngr.= '<td>'.$passengers['currentstatus'].'</td>';
        $psngr.='</tr>';
    }

    echo $pnr.'#'.$psngr;
}

答案 1 :(得分:0)

尝试 -

$psngr .= '<tr>';

$psngr = '';放在外循环之外。