Sort array by value

I am creating a "Pinned" function for my forum, and I am looking for a way to put my attached topics at the beginning of the array so that they "get stuck" at the top of the page.

If the topic is not fixed, then topic_pinned=NULLif it is fixed, then topic_pinned=0.

Array sorted by topic_updated. Attached topics should be sorted by topic_updated, staying at the top of the page, and then under pinned topics - non-pinned topics that are also sorted by topic_updated.

An array of topics ( $forum_topic_results):

Array
(
    [0] => Array
        (
            [topic_id] => 4
            [topic_subject] => Test Subject #4
            [topic_date] => 2015-09-10 18:34:18
            [topic_by] => 1
            [topic_pinned] => 
            [topic_updated] => 2015-09-10 20:37:22
        )

    [1] => Array
        (
            [topic_id] => 3
            [topic_subject] => Test Subject #3
            [topic_date] => 2015-08-22 09:24:40
            [topic_by] => 1
            [topic_pinned] => 0
            [topic_updated] => 2015-09-04 22:02:31
        )

    [2] => Array
        (
            [topic_id] => 2
            [topic_subject] => Test Subject #2
            [topic_date] => 2015-08-15 10:56:00
            [topic_by] => 1
            [topic_pinned] => 
            [topic_updated] => 2015-09-04 19:45:32
        )

    [3] => Array
        (
            [topic_id] => 1
            [topic_subject] => Test Subject #1
            [topic_date] => 2015-08-30 19:48:17
            [topic_by] => 1
            [topic_pinned] => 0
            [topic_updated] => 2015-09-03 00:44:38
        )
)

PHP:

/**
 * getAllTopics
 *
 * Retreives the topics of the chosen category from the `forum_topics` table.
 *
 * @param   $cat_id
 * @access  public
 */
public function getAllTopics($cat_id=NULL)
{
    $database=$this->database;

    $database->query('SELECT topic_id, topic_subject, topic_date, topic_by, topic_pinned, topic_locked FROM forum_topics WHERE topic_cat = :catid ORDER BY topic_updated DESC', array(':catid' => $cat_id));
    $result = $database->statement->fetchAll(PDO::FETCH_ASSOC);

    return $result;
}

# Get topics
$forum_topic_results = $this->getAllTopics($_GET['cat']);
foreach($forum_topic_results as $forum_topic_row)
{
    # Get user username.
    $topic_by=SearchUser($forum_topic_row['topic_by']);

    $data.='<tr>'.
        '<td>'.
            '<h3><a href="'.$_SERVER['PHP_SELF'].'?action=forum_posts&topic='.$forum_topic_row['topic_id'].'">'.$forum_topic_row['topic_subject'].'</a></h3>'.
            'by '.$topic_by['username'].' on '.date('D M d, Y g:i a', strtotime($forum_topic_row['topic_date'])).
        '</td>'.
    '</tr>';
}

Result I want:

Array
(
    [0] => Array
        (
            [topic_id] => 3
            [topic_subject] => Test Subject #3
            [topic_date] => 2015-08-22 09:24:40
            [topic_by] => 1
            [topic_pinned] => 0
            [topic_updated] => 2015-09-04 22:02:31
        )

    [1] => Array
        (
            [topic_id] => 1
            [topic_subject] => Test Subject #1
            [topic_date] => 2015-08-30 19:48:17
            [topic_by] => 1
            [topic_pinned] => 0
            [topic_updated] => 2015-09-03 00:44:38
        )

    [2] => Array
        (
            [topic_id] => 4
            [topic_subject] => Test Subject #4
            [topic_date] => 2015-09-10 18:34:18
            [topic_by] => 1
            [topic_pinned] => 
            [topic_updated] => 2015-09-10 20:37:22
        )

    [3] => Array
        (
            [topic_id] => 2
            [topic_subject] => Test Subject #2
            [topic_date] => 2015-08-15 10:56:00
            [topic_by] => 1
            [topic_pinned] => 
            [topic_updated] => 2015-09-04 19:45:32
        )
)
+4
source share
3 answers

The opposite way.

Mark the PINNED database table as 1.

NOT PINNED as 0.

( ORDER BY) , u :

ORDER BY `topic_pinned` DESC, `topic_updated` DESC
+4

, php, usort. .

usort , , . , , , , :

, , , .

- ( , )

function cmp($a, $b) {
    if ($a['topic_pinned'] == $b['topic_pinned']) {
        // Have to compare Dates
        $adate = new DateTime($a['topic_updated']);
        $bdate = new DateTime($b['topic_updated']);
        return ($adate < $bdate) ? -1 : 1;
    } elseif ($a['topic_pinned'] == null) {
        return 1;
    } else {
        return -1;
    }  
}

usort($array, 'cmp');

, - datetime datetime, .

, sql // .

+1

This should work:

<?php
$finalArr = $forum_topic_results;
foreach ($finalArr as $key => $row) {
    $topic_pinned[$key] = $row['topic_pinned'];
}
array_multisort($topic_pinned, SORT_DESC, $finalArr);
return $finalArr;
?>

Additional Info array_multisort: array_multisort

0
source

Source: https://habr.com/ru/post/1606901/


All Articles