0

我想从一个组织为 Key => value 的数组中填充一个 html 表,我使用 zend 框架,我的问题是当我必须在 html 代码中打印它们时,Php 不尊重数组中值的顺序这是我的控制器:

<?php

class Dashboard_LogsController extends Dashboard_Library_Custom_Controller_Dashboard
{   
    function array_merge_rec($arrays) 
    {
        $result = array();

        $keys = array('delivred','open', 'click', 'unsubscribe', 'soft', 'hard', 'deferred');

        foreach ($arrays as $key => $value) {
            if (is_array($value)) {
                foreach ($value as $item) {
                    $result[$item['isp']][$key] = $item['count'];
                }
            }
        }

        $results = array();

        foreach($result as $key => $value)
        {
            foreach($keys as $k)
            {
                if(!isset($result[$key][$k])) $result[$key][$k] = 0;
            }
        }

        return $result;
    }

    public function logsAction()
    {   
        $this->view->headLink()->appendStylesheet(STATIC_URL . '/css/bootstrap.datetimepicker.min.css');
        $this->view->headScript()->appendFile(STATIC_URL . '/js/bootstrap.datetimepicker.min.js');


        $log_review = new Dashboard_Model_DbTable_logs();


        $open = $log_review->getOpen();
        $click = $log_review->getClick();
        $unsubscribe = $log_review->getUnsubscribe();
        $deferred = $log_review->getDeferred();
        $delivred = $log_review->getSent();
        $SB = $log_review->getSB();
        $HB = $log_review->getHB();

        $result = $this->array_merge_rec(array('delivred'=>$delivred,'open' => $open, 'click' => $click, 'unsubscribe' => $unsubscribe, 'hard' => $HB, 'soft' => $SB, 'deferred'=>$deferred));

        $this->view->data = $result; 
    }



}

我用来在屏幕上显示表格的 html 代码如下:

      <div class="row-fluid">
     <div class="span12 meter-legend meter-statistics">
        <table>
        <tr>
        <th>ISP</th><th>Email address</th><th>Delivred</th><th>Open</th><th>Clicks</th><th>Unsubscribed</th><th>Hard</th><th>Soft</th><th>Deferred</th>
        </tr>
        <?php foreach($this->data as $name => $isp) { ?>
        <?php
        $sum =0;
         foreach($isp as $key => $value) { 
        $sum =$sum+$value;
        } 
        if ($sum >= 500) {?>
        <tr>
        <td><?php echo $name; ?></td>
        <td><?php echo $sum; ?></td>
        <?php foreach($isp as $key => $value) { ?>
        <td><?php echo $value; ?></td>
        <?php } ?>
        </tr>
        <?php } ?>

        <?php } ?>
        </table>

所以这段代码填充了表格,但不尊重列的顺序,我真的不知道 php 是如何做到这一点的。我的问题是,如果有人能告诉我如何在第一次...重复中打印数组中的第一个结果。而且我不知道我是否可以使用键来选择数组值,但是如果可以的话,我真的不知道该怎么做。

4

3 回答 3

1

打印前订购阵列。这个特定的将按键排序阵列,从低到高,但请查看 Malcom 发布的链接以查看所有选项。

<?php 
    ksort($this->data);         
    foreach($this->data as $name => $isp) {       
    // YOUR CODE HERE
   } 
 ?>
于 2013-11-05T09:55:54.267 回答
1

如果您只是想在循环遍历数组之前对其进行排序,PHP 为您提供了一些选择:

http://php.net/manual/en/array.sorting.php

选择适合你的排序函数,让你的数组按照你想要的方式排序,然后你可以毫无问题地执行你的 foreach() 循环。

于 2013-11-05T09:48:56.103 回答
1

php按键排序数组。在这种情况下,您应该使用 int 值作为数组的键。对于一个好的解决方案,您可以使用 MACRO:

常量交付 = 0; 常开 = 1; ...

$array[self::DELIVERED]

于 2013-11-05T09:49:51.637 回答