Jump to content

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!

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.