0

Im trying to create sort button for list but ive got this list, and i dont know what to do with this timestamp is it possible to sort with it, here is my array type, i hope you will help me guys here it it:

Array
(
    [0] => 17
    [ticket] => 17
    [1] => Name Second Name
    [author] => Name Second Name 
    [2] => modules
    [subject] => modules
    [3] => 1412335833
    [timestamp] => 1412335833
    [4] => 1414048041
    [activity] => 1414048041
    [5] => Closed
    [type] => Closed
    [6] => 3
    [priority] => 3
    [7] => 13
    [assignment] => 13
    [8] => 17
)

Array
(
    [0] => 18
    [ticket] => 18
    [1] => Name Second Name 
    [author] => Name Second Name
    [2] => modules
    [subject] => modules
    [3] => 1412335935
    [timestamp] => 1412335935
    [4] => 1414048095
    [activity] => 1414048095
    [5] => Closed
    [type] => Closed
    [6] => 3
    [priority] => 3
    [7] => 0
    [assignment] => 0
    [8] => 18
)

As you can see timestamp is the date. I need to sort it, here is what ive tried:

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

But i get error because of timestamp... Here is error Warning: Illegal string offset 'timestamp' in /home/...../domains/foxiad.com/public_html/...../modules/ticketsmith/index.php on line 365

8
  • 1
    What error you are getting? Commented Jul 9, 2015 at 9:18
  • delete the strtotime() around the timestamp Commented Jul 9, 2015 at 9:19
  • Deleted, same error here it is Warning: Illegal string offset 'timestamp' in /home/...../domains/foxiad.com/public_html/...../modules/ticketsmith/index.php on line 365 Commented Jul 9, 2015 at 9:20
  • usort($row, 'date_compare'); what is that? Commented Jul 9, 2015 at 9:21
  • 1
    possible duplicate of Reference: all basic ways to sort arrays and data in PHP Commented Jul 9, 2015 at 9:28

1 Answer 1

1

The output of array you are showing here looks generated by this kind of thing, which are all seperate arrays.

while($row = mysql_fetch_row($variable))
{
    print_r($row);
}

You need to build one seprate array, then perform a sort on it.

while($row = mysql_fetch_row($variable))
{
    $results[] = $row;
}

Then, you will be able to sort that, feeding $results to usort()

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.