douyicao2199 2013-11-05 09:32
浏览 57
已采纳

PHP按顺序显示key => value数组

I want to fill an html table from an array organized as Key => value, i use zend Framework, and my problem here that Php didn't respect the order of the value in the array when i have to print them in an html code and this is my controller:

<?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; 
    }



}

and the html code that i used to show the table on the screen is the follow:

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

So this code fill the table but without respecting the order of the column, and i don't really know how php do this. My question here if anyone can tell me how to do to print the first result in the array in the first ... repetition. and i don't know if i can use the key to choose the array value, but if we can i don't really know how to do it.

  • 写回答

3条回答 默认 最新

  • duancuisan2503 2013-11-05 09:48
    关注

    If you're simply trying to order your array before cycling through it, PHP has a few options for you:

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

    Pick the sorting function that works for you, get your array ordered the way you want it, then you can do your foreach() loop without issue.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?