-3

I have this array filled with data from a database

$collectTable1 = array( 'errand' => $interest->errand_id,
                        'timestamp' => $interest->timestamp,
                        'type' => $interest->type,
                        'amount' => $interest->amount
                    );

$collector[] = $collectTable1; 

And i want to rsort the timestamp, like this

$sortTime = rsort($collectedData['timestamp']);

I tried this, and i get this output

 function timesort($a, $b) {


 return (intval($a['timestamp']) > intval($b['timestamp']));
}

usort($collector, "timesort");

2017-12-01 10:53:26

I tought i would get from the descending date point? Something like 2018-09-04 12:32:16.

My timestamp also contains both unixtimestamp and regular dates like this " 2017-12-01 10:53:26"

Pontus
  • 61
  • 7
  • use `rsort($collectedData)` as `$collectedData['timestamp']` is single value not an array instead `$collectedData` is an array – Leena Patel Nov 01 '18 at 09:27
  • 3
    Dont know, what is `$collectedData`? Could it be a TYPO of `$collector` – RiggsFolly Nov 01 '18 at 09:28
  • you can use `is_array` to check - https://secure.php.net/manual/en/function.is-array.php – treyBake Nov 01 '18 at 09:28
  • I think you probably mean you want to sort the array on timestamp and not as you are attempting to do.... sort the timestamp – RiggsFolly Nov 01 '18 at 09:30
  • 1
    Probably you are only retaining one row of data from the DB, this looks like the symptom of a bigger issue. `I have this array filled with data from a database` when you fetch the data use `fetchAll` or make sure your adding it with `$collectTable1[] = $row` in "typically" a while loop Or just sort the data with the SQL query itself (which wont solve the "possible" fetching issue) – ArtisticPhoenix Nov 01 '18 at 09:30
  • Where are you defining `$collectedData`? – Harvey Fletcher Nov 01 '18 at 09:33
  • They are probably still working with https://stackoverflow.com/questions/53086327/array-wont-nestle-another-array – Nigel Ren Nov 01 '18 at 09:50
  • Possible duplicate of [How can I sort arrays and data in PHP?](https://stackoverflow.com/questions/17364127/how-can-i-sort-arrays-and-data-in-php) – splash58 Nov 01 '18 at 10:01
  • @Pontus Did you manage to solve the issue? – dWinder Nov 01 '18 at 11:01
  • @DavidWinder No i did not manage it:P Still trying tho! – Pontus Nov 01 '18 at 12:37
  • @DavidWinder i edited my question :P – Pontus Nov 01 '18 at 13:09
  • @Pontus Sorry - I still cannot fully understand your question... what is your desire output? please post your full code and maybe I will be able to find the problem – dWinder Nov 01 '18 at 13:30
  • @DavidWinder All i want is to sort the timestamp from the latest(Desc) to the oldest time So from 2018-11-01 to the oldest datetime in the array. – Pontus Nov 01 '18 at 13:36

3 Answers3

2

I guessing you have array of element in $collector.

If you want to sort those by the timestamp you can use usort

Consider the following example:

$collector = array();
$e1 = array("errand" => 1, "timestamp" => "2017-12-01 10:53:26");
$e2 = array("errand" => 2, "timestamp" => "2018-07-01 10:53:26");
$e3 = array("errand" => 3, "timestamp" => "2018-12-01 10:53:26");

$collector = array($e1, $e2, $e3);

function cmp($a, $b)
{
    return (strtotime($a['timestamp']) < strtotime($b['timestamp']));
}

usort($collector, "cmp");

When your timestamp values are in string use strtotime to convert them to EPOC before compare.

Now, the $collector array elements are sorted by the timestamp value.

Output of the code example is:

Array
(
    [0] => Array
        (
            [errand] => 3
            [timestamp] => 2018-12-01 10:53:26
        )
    [1] => Array
        (
            [errand] => 2
            [timestamp] => 2018-07-01 10:53:26
        )
    [2] => Array
        (
            [errand] => 1
            [timestamp] => 2017-12-01 10:53:26
        )
)
dWinder
  • 11,359
  • 3
  • 19
  • 33
0

Once you have your array:

<?php

$data = [
    ['timestamp' => '100'],
    ['timestamp' => '300'],
    ['timestamp' => '200']
];

usort($data, function($a, $b) {
    return $b['timestamp'] <=> $a['timestamp'];
});

var_export($data);

Output:

array (
    0 => 
    array (
    'timestamp' => '300',
    ),
    1 => 
    array (
    'timestamp' => '200',
    ),
    2 => 
    array (
    'timestamp' => '100',
    ),
)
Progrock
  • 6,765
  • 1
  • 16
  • 25
  • @splash58, it's all useful information to the answer as OP or future readers may have earlier versions of PHP (although I hope that will improve) – Nigel Ren Nov 01 '18 at 09:58
0

If you have multiple value for all key of associative array $collectTable1 than

foreach($interest as $i){
$collectTable1 = array( 'errand' => array($i->errand_id),
                        'timestamp' => array($i->timestamp),
                        'type' => array($i->type),
                        'amount' => array($i->amount)
                    );


rsort($collectTable1[‘timestamp’]);

Here collectTable1 is an associative array of one dimensional arrays ie.

$collectTable1[‘timestamp’][0]=firstvalue $collectTable1[‘timestamp’][1]=secondvalue

And so on

Ram Bhandari
  • 469
  • 1
  • 4
  • 18