Asked  7 Months ago    Answers:  5   Viewed   48 times

I have an array such as:

Array
(
[0] => Array
    (
        [id] => 2
        [type] => comment
        [text] => hey
        [datetime] => 2010-05-15 11:29:45
    )

[1] => Array
    (
        [id] => 3
        [type] => status
        [text] => oi
        [datetime] => 2010-05-26 15:59:53
    )

[2] => Array
    (
        [id] => 4
        [type] => status
        [text] => yeww
        [datetime] => 2010-05-26 16:04:24
    )

)

Can anyone suggest a way to sort/order this based on the datetime element?

 Answers

19

Use usort() and a custom comparison function:

function date_compare($a, $b)
{
    $t1 = strtotime($a['datetime']);
    $t2 = strtotime($b['datetime']);
    return $t1 - $t2;
}    
usort($array, 'date_compare');

EDIT: Your data is organized in an array of arrays. To better distinguish those, let's call the inner arrays (data) records, so that your data really is an array of records.

usort will pass two of these records to the given comparison function date_compare() at a a time. date_compare then extracts the "datetime" field of each record as a UNIX timestamp (an integer), and returns the difference, so that the result will be 0 if both dates are equal, a positive number if the first one ($a) is larger or a negative value if the second argument ($b) is larger. usort() uses this information to sort the array.

Tuesday, June 1, 2021
 
julesj
answered 7 Months ago
65

You need to use usort, a function that sorts arrays via a user defined function. Something like:

function cmp($a, $b)
{
    if ($a["price"] == $b["price"]) {
        return 0;
    }
    return ($a["price"] < $b["price"]) ? -1 : 1;
}

usort($yourArray,"cmp")
Wednesday, March 31, 2021
 
JohnnyW
answered 9 Months ago
11

You can do calculation and sorting in your query:

SELECT 
    quiz_name, 
    quiz_attempts, 
    cumulative_score,
    (cumulative_score/quiz_attempts) as score_avg
FROM scoredata
ORDER BY score_avg DESC
Wednesday, March 31, 2021
 
Juriy
answered 9 Months ago
19

Your array has a typical flat structure encoding a hierarchy. But only if you fix the error in there.

It basically then works like in the following list with PHP codes, including exceptions telling where errors are in the data so that you can fix your data (you can find similar examples on this website e.g. see the linked questions in How can I convert a series of parent-child relationships into a hierarchical tree?):

  1. Initialize a root entry inside a tree array. There you can add children to.

    $tree     = ['children' => []];
    
  2. Create an array of pointers into the tree of which it's first element (level 0) points to the root element of the tree:

    $pointers = [&$tree];
    
  3. Now go over each line inside the data:

    foreach ($data as $index => $line) {
    
  4. Decide whether the line closes and store that decisions result:

    $close = '/' === $line[0];
    
  5. Store the current count of pointers:

    $count = count($pointers);
    
  6. In case the line does not close, open a new element and continue:

    $pointers[$count]                   = ['tag' => $line, 'children' => []];
    $pointers[$count - 1]['children'][] = & $pointers[$count];
    continue;
    
  7. In case the line does close instead, validate if the tagname is matches and if so, remove the last created pointer:

    if ($count === 1) {
        throw new RuntimeException('Can not close on lowest level.');
    }
    
    $name = $pointers[$count - 1]['tag'];
    
    if ($name !== substr($line, 1)) {
        throw new RuntimeException(
            "Invalid closing element <$line> (line #$index) in <$name>"
        );
    }
    
    array_pop($pointers);
    
  8. After all lines have been processed like outlined all pointers can be removed:

    unset($pointers);
    
  9. The result is the array that can be found in the first children in the root node. It can be assigned to a variable and the not needed references can be deleted then:

    $result = &$tree['children'][0];
    unset($tree);
    print_r($result);
    

If the data is correct, it exemplary outputs:

Array
(
    [tag] => av_one_third
    [children] => Array
        (
            [0] => Array
                (
                    [tag] => av_icon_box
                    [children] => Array
                        (
                        )
                )
            [1] => Array
                (
                    [tag] => av_button
                    [children] => Array
                        (
                        )
                )
            [2] => Array
                (
                    [tag] => av_icon_box
                    [children] => Array
                        (
                        )
                )
        )
)
Saturday, May 29, 2021
 
Lloydworth
answered 7 Months ago
71

Using uksort:

uksort($array, function($a, $b) { return count($b) - count($a); });

Using array_multisort:

array_multisort(array_map('count', $array), SORT_DESC, $array);

With PHP < 5.3:

function sort_cb($a, $b) {
    return count($b) - count($a);
}
uksort($array, 'sort_cb');
Thursday, July 29, 2021
 
employeegts
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share