Jump to content

Is there an easy way to sort pairs?


aeroswat

Recommended Posts

Is there a function that will sort pairs in an array? I have an array of arrays. Data is in there like this

 

arr[0][0] = timestamp1

arr[0][1] = data1

arr[1][0] = timestamp2

arr[1][1] = data2

etc

 

I want to sort my pairs by the timestamp so that all of the newest timestamps will be on top and oldest on bottom. Is there a function in php that does this or a quick work around with several functions I can use?

Link to comment
https://forums.phpfreaks.com/topic/206885-is-there-an-easy-way-to-sort-pairs/
Share on other sites

Hi,

 

it will be more lucky if timestamps are in an other array.

 

Heres an example with array_multisort function:

 

$array[0][0] = timestamp;
$array[0][1] = data;
$array[1][0] = timestamp;
$array[1][1] = data;
$array[2][0] = timestamp;
$array[2][1] = data;

$sorter = array();
foreach ($array as $item)
{
            $sorter[] = $item[0];
}

array_multisort($array, $sorter);

Hi,

 

it will be more lucky if timestamps are in an other array.

 

Heres an example with array_multisort function:

 

$array[0][0] = timestamp;
$array[0][1] = data;
$array[1][0] = timestamp;
$array[1][1] = data;
$array[2][0] = timestamp;
$array[2][1] = data;

$sorter = array();
foreach ($array as $item)
{
            $sorter[] = $item[0];
}

array_multisort($array, $sorter);

 

Thanks. That didn't seem to work out for me the way that I was doing it so I decided to just split in separate arrays and use the multisort that way. Thank you for the help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.