Jump to content

Sorting An Array ( Jumping Indexs )


lolacow

Recommended Posts

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 by lolacow
Link to comment
Share on other sites

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 ) ... )

Link to comment
Share on other sites

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 by lolacow
Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.