Jump to content

Recommended Posts

Hi.

I have a multidimensional array and i need sort it by two different key.

I've tried almost every sort function but no chance.

 

Array ( 
[0] => Array ( 
	[0] => 1 [id] => 1 
	[11] => this is test [main_txt] => this is test 
	[12] => 943965420 [add_time] => 943965420 
	[13] => 1323356400 [add_date] => 1323356400 ) 
[1] => Array ( 
	[0] => 3 [id] => 3 
	[11] => another test [main_txt] => another test
	[12] => 943965120 [add_time] => 943965120 
	[13] => 1323702000 [add_date] => 1323702000 ) 
[2] => Array ( 
	[0] => 32 [id] => 32 
	[11] => oppss one more test [main_txt] => oppss one more test
	[12] => 943944900 [add_time] => 943944900 
	[13] => 1323702000 [add_date] => 1323702000 ) 
[3] => Array ( 
	[0] => 5 [id] => 5 
	[11] => okay, this is last one. Seriously [main_txt] => okay, this is last one. Seriously
	[12] => 943937160 [add_time] => 943937160 
	[13] => 1323615600 [add_date] => 1323615600 )
)		

 

I need sort by add_date(Newer to Older) first then add_time(Later to Earlier) later.

 

I hope i made my self clear.

 

 

Link to comment
https://forums.phpfreaks.com/topic/253067-sorting-multidimensional-array/
Share on other sites

If you are asking created function? then asnwer will be No.

I did not do/write any codes besides using standard sorting function of php.

 

I will be glad if you guide me and/or give me a logical idea. I can do rest...

usort() is the function you want.  It requires a callback which will compare the two array elements.  Seeing as you said you already tried it, I can only assume you attempted to make that callback function.  Post what you tried and we can help you fix it.

 

The problem starts right here. Because i don't have any function. (previously i used, usort($arr); )

 

You have to create the function.  Ex:

function mySortCmp($a, $b){
///..do any comparisons here
//..return -1 if $a is less than $b
//..return 0 if $a is the same as $b
//..return 1 if $a is greater than $b
}


usort($arr, 'mySortCmp');

 

PHP will call your function several times, each time it will pass two elements of the array ($a and $b).  Your function has to compare them and return a value which is either less than 0, 0, or greater than 0...depending on how $a compares to $b.

 

So, create a function which will compare your elements, first by the add_date then by the add_time.

 

 

Thanks for information.

You wrote "first by the add_date then by the add_time." Also i mention this in my first post.

 

But there is a problem such as,

 

ID | Add Date        | Add Time

---+-------------------+------------

1  |  2011/12/12    |  12:50

2  |  2011/12/12    |  14:38

3  |  2011/12/11    |  13:00

4  |  2011/12/10    |  10:53

 

in my logic entry #4 will be output as first record.

 

Don't you think?

I need sort by add_date(Newer to Older) first then add_time(Later to Earlier) later

 

Both fields are DESC and apparently from a database (you are using mysql_fetch_array and getting both the numerical and associative keys.) Why not just do this in the query -

 

ORDER BY add_date DESC, add_time DESC

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.