lolacow Posted November 16, 2012 Share Posted November 16, 2012 (edited) Hello, I am trying to sort an Array which I obtained from the explode function. But I want to sort it by either the index of if %2 == true or false. I explain : I would like to know if it is possible to sort the array ( see below ) in order so that not the only the time moves but the song tittle aswell. Same thing for the tittle, if I want to sort using tittle, I want not only the tittle to move, but the times aswell. This is my array un-sorted : Array ( [0] => 3:31 [1] => Oops!...I Did It Again [2] => 3:23 [3] => Stronger [4] => 3:43 [5] => Don't Go Knocking' on My Door [6] => 4:25 [7] => (I Can't Get No) Satisfaction [8] => 3:50 [9] => Don't Let Me Be the Last to Know [10] => 3:36 [11] => What U See(Is What U Get) [12] => 3:26 [13] => Lucky [14] => 3:25 [15] => One Kiss from You [16] => 4:39 [17] => Where Are You Now [18] => 3:17 [19] => Can't Make You Love Me [20] => 4:29 [21] => When Your Eyes Say It [22] => 2:46 [23] => Dear Diary ) This is the result I get with asort() , and I do not want this. I would like the song to be indexed with their times but the times to be sorted ascendingly. Array ( [18] => 3:17 [0] => 3:31 [10] => 3:36 [6] => 4:25 [7] => (I Can't Get No) Satisfaction [22] => 2:46 [2] => 3:23 [14] => 3:25 [12] => 3:26 [4] => 3:43 [8] => 3:50 [20] => 4:29 [16] => 4:39 [19] => Can't Make You Love Me [23] => Dear Diary [5] => Don't Go Knocking' on My Door [9] => Don't Let Me Be the Last to Know [13] => Lucky [15] => One Kiss from You [1] => Oops!...I Did It Again [3] => Stronger [11] => What U See(Is What U Get) [21] => When Your Eyes Say It [17] => Where Are You Now ) I don't think asort is what I should be using in here because I can't seems to find a way to make it sort correctly. Thanks alot if anyone can help, Marc Edited November 16, 2012 by lolacow Quote Link to comment https://forums.phpfreaks.com/topic/270807-sorting-an-array-jumping-indexs/ Share on other sites More sharing options...
requinix Posted November 16, 2012 Share Posted November 16, 2012 array_chunk it so you have a multi-dimensional array, Array ( [0] => Array ( [0] => 3:31 [1] => Oops!...I Did It Again ) [1] => Array ( [0] => 3:23 [1] => Stronger ) ... ) then usort it so the subarrays are sorted by time. Array ( [0] => Array ( [0] => 2:46 [1] => Dear Diary ) [1] => Array ( [0] => 3:17 [1] => Can't Make You Love Me ) ... ) Quote Link to comment https://forums.phpfreaks.com/topic/270807-sorting-an-array-jumping-indexs/#findComment-1393074 Share on other sites More sharing options...
Barand Posted November 16, 2012 Share Posted November 16, 2012 Once you have "chunked" it you can just use sort() without a custom function. If you you sort a 2D array the subarrays will, by default, be sorted on the values of the first element in each. Quote Link to comment https://forums.phpfreaks.com/topic/270807-sorting-an-array-jumping-indexs/#findComment-1393079 Share on other sites More sharing options...
lolacow Posted November 16, 2012 Author Share Posted November 16, 2012 (edited) Thanks alot for the answers! But I might be doing something wrong but my array_chunk($audio, 2); isn't working. Edit: Nvm got it working. Now to make it in an html table, thanks alot ^-^. Huh, is there a way to make it back into an array o.o? Edited November 16, 2012 by lolacow Quote Link to comment https://forums.phpfreaks.com/topic/270807-sorting-an-array-jumping-indexs/#findComment-1393082 Share on other sites More sharing options...
requinix Posted November 16, 2012 Share Posted November 16, 2012 Only problem with sort() is that times like "10:00" will be sorted before "9:00" (because 1 Quote Link to comment https://forums.phpfreaks.com/topic/270807-sorting-an-array-jumping-indexs/#findComment-1393086 Share on other sites More sharing options...
Barand Posted November 16, 2012 Share Posted November 16, 2012 (edited) try <?php $a = array ( 0 => '3:31', 1 => 'Oops!...I Did It Again', 2 => '3:23', 3 => 'Stronger', 4 => '3:43', 5 => 'Don\'t Go Knocking\' on My Door', 6 => '4:25', 7 => '(I Can\'t Get No) Satisfaction', 8 => '3:50', 9 => 'Don\'t Let Me Be the Last to Know', 10 => '3:36', 11 => 'What U See(Is What U Get)', 12 => '3:26', 13 => 'Lucky', 14 => '3:25', 15 => 'One Kiss from You', 16 => '4:39', 17 => 'Where Are You Now', 18 => '3:17', 19 => 'Can\'t Make You Love Me', 20 => '4:29', 21 => 'When Your Eyes Say It', 22 => '2:46', 23 => 'Dear Diary' ); $b = array_chunk($a, 2); sort($b ); echo '<pre>'.print_r($b, 1).'</pre>'; ?> RESULTS: Array ( [0] => Array ( [0] => 2:46 [1] => Dear Diary ) [1] => Array ( [0] => 3:17 [1] => Can't Make You Love Me ) [2] => Array ( [0] => 3:23 [1] => Stronger ) [3] => Array ( [0] => 3:25 [1] => One Kiss from You ) [4] => Array ( [0] => 3:26 [1] => Lucky ) [5] => Array ( [0] => 3:31 [1] => Oops!...I Did It Again ) [6] => Array ( [0] => 3:36 [1] => What U See(Is What U Get) ) [7] => Array ( [0] => 3:43 [1] => Don't Go Knocking' on My Door ) [8] => Array ( [0] => 3:50 [1] => Don't Let Me Be the Last to Know ) [9] => Array ( [0] => 4:25 [1] => (I Can't Get No) Satisfaction ) [10] => Array ( [0] => 4:29 [1] => When Your Eyes Say It ) [11] => Array ( [0] => 4:39 [1] => Where Are You Now ) ) Edited November 16, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/270807-sorting-an-array-jumping-indexs/#findComment-1393089 Share on other sites More sharing options...
lolacow Posted November 16, 2012 Author Share Posted November 16, 2012 And then I could just loop over it and merge it to get back to the original array ? Or am I mistaken, because after it got sorted, I would like to make it back into the original form because my function to print it as a table uses the others form. ( one dimension array ) And by the way, thanks alot for sharing ^-^, this is greatly apreciated. Quote Link to comment https://forums.phpfreaks.com/topic/270807-sorting-an-array-jumping-indexs/#findComment-1393092 Share on other sites More sharing options...
Barand Posted November 16, 2012 Share Posted November 16, 2012 yes - just $c = array(); foreach ($b as $sub_B) { $c = array_merge($c,$sub_B); } $c is now in your orig format Quote Link to comment https://forums.phpfreaks.com/topic/270807-sorting-an-array-jumping-indexs/#findComment-1393099 Share on other sites More sharing options...
lolacow Posted November 16, 2012 Author Share Posted November 16, 2012 (edited) Works like a charm. ^-^ Thanks for all your help. Edit: Is there a way to sort on 2nd value with the function sort? Edit2: Nvm found a way to do it. Edited November 16, 2012 by lolacow Quote Link to comment https://forums.phpfreaks.com/topic/270807-sorting-an-array-jumping-indexs/#findComment-1393104 Share on other sites More sharing options...
Barand Posted November 16, 2012 Share Posted November 16, 2012 To sort on the 2nd you would need a custom user sort function mysort($a , $b ) { return strcmp($a[1], $b[1]); } usort ($b, 'mysort'); // replaces the current sort() Quote Link to comment https://forums.phpfreaks.com/topic/270807-sorting-an-array-jumping-indexs/#findComment-1393109 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.