繁体   English   中英

在PHP的foreach循环中显示两个数组?

[英]Display two arrays in foreach loop in Php?

我想在单个foreach循环中显示数据。 我有两个表dailystatsmonthlystats ,它们都有相同的列,如callsminutesincomingcalls等。我使用Yii PHP Framework,这是我的控制器

public function actionViewStats(){
    $model = new Company();
    $id = $_GET['id'];

    $modelMonthly = $model->getCompanyUsageMonthly($id);
    $modelDaily = $model->getCompanyUsageDaily($id);

    $this->renderPartial('/shared/_company_stats', array(
        'modelDaily' => $modelDaily,
        'modelMonthly'=>$modelMonthly
        ));
}

这是我的桌子视图。

       <?PHP  if(isset($modelMonthly) && sizeof($modelMonthly)!=0): ?>
            <div class="ibox-content col-md-12 col-xs-12">
                <div class="title col-md-2">
                    <h3>Current Usage</h3>
                </div>
                <div class="col-md-10">
                    <div class="col-md-12 col-xs-12">

                        <table class="table">
                            <thead>
                            <th>Minutes</th>
                            <th>Incoming Minutes</th>
                            <th>Calls</th>
                            <th>Incoming Calls</th>
                            </thead>
                            <tbody>
                            <?PHP
                            if(isset($modelMonthly)){
                                foreach($modelMonthly as $monthlystats){
                                    ?>
                                    <tr>

                                        <td><?php echo $monthlystats->minutes?></td>
                                        <td><?PHP echo $monthlystats->incoming_minutes; ?></td>

                                        <td><?PHP echo $monthlystats->calls; ?></td>
                                        <td><?PHP echo $monthlystats->incoming_calls; ?></td>

                                    </tr>
                                <?PHP } } ?>
                            </tbody>
                        </table>

                    </div>
                </div>
            </div>
        <?PHP endif;?>

这只显示每月统计modelMonthly但我也想在同一foreach循环中显示每日统计modelDaily ..我该怎么做? 我试过array_combine等,但无法做到。 在SOF上搜索,但无法找到解决方案。

我上面的代码仅显示这样的每月统计信息

在此处输入图片说明

但是我想在下面显示这样。 我已经制作了表格,但无法在同一foreach循环中同时使用modelDailymodelMonthly 我尝试了其他事情,但无法做到。

在此处输入图片说明

<?PHP
                                if(isset($modelMonthly)){
                                    foreach($modelMonthly as $usage){
                                        ?>
                                        <tr>

                                            <td><?php echo $usage->minutes?></td>
                                            <td><?PHP echo $usage->incoming_minutes; ?></td>

                                            <td><?PHP echo $usage->calls; ?></td>
                                            <td><?PHP echo $usage->incoming_calls; ?></td>

                                        </tr>
                                    <?PHP } } ?>

如果两个数组都具有数字索引,则可以使用常规的for循环:

for ($i = 0; $i < max(count($modelMonthly), count($modelDaily)); $i) {
     if (isset($modelDaily[$i]) {
         // Add the daily columns
     } else {
         // Add as much empty columns
     }
     if (isset($modelMonthly[$i]) {
         // Add the monthly columns
     } else {
         // Add as much empty columns
     }
}

下一步是在thead添加所需的标头th ,并在循环内添加额外的td


另外,您可以独立创建两个表,并使用CSS定位它们。 它不在此问题的范围内,但这是我的建议。

您应该使用此技术

foreach($modelMonthly as $index => $usage){
        ?>
        <tr>

            <td><?php echo $usage->minutes?></td>
            <td><?PHP echo $usage->incoming_minutes; ?></td>

            <td><?PHP echo $usage->calls; ?></td>
            <td><?PHP echo $usage->incoming_calls; ?></td>

            <td><?php if (isset($dailystats[$index]->minute)) echo $dailystats[$index]->minutes?></td>
            <td><?PHP if (isset($dailystats[$index]->incoming_minutes)) echo $dailystats[$index]->incoming_minutes; ?></td>

            <td><?PHP if (isset($dailystats[$index]->calls)) echo $dailystats[$index]->calls; ?></td>
            <td><?PHP if (isset($dailystats[$index]->incoming_calls)) echo $dailystats[$index]->incoming_calls; ?></td>
        </tr>
    <?PHP } } ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM