php - How to sort a massive/object

723

I've got a massive$events. To output information of this massive I do:

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

Interesting that I may use both$user->id and$user['id'] sintax. But never mind. How can I sort this massive by$user['id'] desc?

Edited: I take data from database, but not usual way, so I can't use sql syntax so sort results:

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

Answer

Solution:

Do you read the data from a database? then simply add to your querySORT BY id DESC

else you may use usort() in PHP:

usort($events, function ($a, $b) { return $a->id - $b->id; });
480

Answer

Solution:

adding anorder() to the query doesn't get you there?

$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

it looks to me like this is all coming from the same table, if so why not build a single query?

Maybe something like:

$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'));

Not real sure if this will help get you there, maybe.

129

Answer

Solution:

This sucks, since you have to go through the list twice, but you could...

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

Now you can use events arr, which is sorted by user_id

foreach ($events_arr as $user_id => $usern) {
  foreach ($usern as $user) {
    echo $user_id . ": " . $user->action;
  }
}
852

Answer

Solution:

Decided to denormolise my DB Table to make the process of selection data easier. Let's see if it get's better.

People are also looking for solutions to the problem: php - Logical Captcha from SQL Table and external check

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.