0

我有一个巨大的$events. 为了输出这个海量的信息,我做:

<?php foreach ($this->events as $usern): ?><?php foreach ($usern as $user): ?>
        <tr>
            <td><?php echo $user['id']; ?></td>
            <td><?php echo $user->resource_types; ?></td>
            <td><?php echo $user->resource_ids; ?></td>
            <td><?php echo $user->action; ?></td>
            <td><?php echo $this->owner($user->originator); ?></td>
            <td><?php echo $user->event_date; ?></td>
            <td>
            <?php //if($this->canManageBusiness($user->id)): ?>

            <?php // endif ?>
            </td>
        </tr>
    <?php endforeach ?><?php endforeach ?>

有趣的是我可以同时使用$user->id$user['id']sintax。但是没关系。我怎样才能按$user['id']desc 对这个海量进行排序?

编辑:我从数据库中获取数据,但不是通常的方式,所以我不能使用 sql 语法对结果进行排序:

$events[] =$this->_dbTable->fetchAll($this->_dbTable->select()->from($this->_dbTable, array('id','resource_types','resource_ids','action','originator','event_date'))->where('resource_types = ?', 'business')->where('resource_ids',$bus->id));
$events[] =$this->_dbTable->fetchAll($this->_dbTable->select()->from($this->_dbTable, array('id','resource_types','resource_ids','action','originator','event_date'))->where('resource_types = ?', 'document-type')->where('resource_ids',$dt->id));
return $events
4

4 回答 4

2

您是否从数据库中读取数据?然后只需添加到您的查询SORT BY id DESC

否则你可以在 PHP 中使用 usort():

usort($events, function ($a, $b) { return $a->id - $b->id; });
于 2013-04-16T12:12:10.217 回答
1

在查询中添加一个order()不会让你到达那里?

$events[] =$this->_dbTable->fetchAll($this->_dbTable->select()
    ->from($this->_dbTable,
        array('id','resource_types','resource_ids','action','originator','event_date'))
    ->where('resource_types = ?', 'business')
    ->where('resource_ids',$bus->id)
    ->order('id DESC'));
$events[] =$this->_dbTable->fetchAll($this->_dbTable->select()
    ->from($this->_dbTable,
        array('id','resource_types','resource_ids','action','originator','event_date'))
    ->where('resource_types = ?', 'document-type')
    ->where('resource_ids',$dt->id)
    ->order('id DESC'));
return $events

在我看来,这一切都来自同一个表,如果是这样,为什么不构建一个查询呢?

也许是这样的:

$events[] = $this->_dbTable->fetchAll($this->_dbTable->select()
              ->from($this->_dbTable,
                   array('id','resource_types','resource_ids','action','originator','event_date'))
              ->where('resource_types IN (?)', array('business', 'document-type'))
              ->where('resource_ids IN (?)', array($bus->id, $dt->id))
              ->order('id DESC'));

不确定这是否会帮助您到达那里,也许。

于 2013-04-17T09:41:16.447 回答
0

决定对我的数据库表进行去规范化,以使选择数据的过程更容易。让我们看看它是否变得更好。

于 2013-04-18T14:38:30.290 回答
0

这很糟糕,因为你必须通过列表两次,但你可以......

$events_arr = array();
<?php foreach ($this->events as $usern): ?><?php foreach ($usern as $user): ?>
    $events_arr[$user_id][] = $usern; endforeach; endforeach;
ksort($events_arr);

现在可以使用事件 arr,按 user_id 排序

foreach ($events_arr as $user_id => $usern) {
  foreach ($usern as $user) {
    echo $user_id . ": " . $user->action;
  }
}
于 2013-04-17T07:46:42.967 回答