aeroswat Posted July 6, 2010 Share Posted July 6, 2010 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? Quote Link to comment https://forums.phpfreaks.com/topic/206885-is-there-an-easy-way-to-sort-pairs/ Share on other sites More sharing options...
bh Posted July 6, 2010 Share Posted July 6, 2010 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); Quote Link to comment https://forums.phpfreaks.com/topic/206885-is-there-an-easy-way-to-sort-pairs/#findComment-1081878 Share on other sites More sharing options...
aeroswat Posted July 6, 2010 Author Share Posted July 6, 2010 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! Quote Link to comment https://forums.phpfreaks.com/topic/206885-is-there-an-easy-way-to-sort-pairs/#findComment-1081967 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.