Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Sorting Array

Status
Not open for further replies.

alsaffar

Programmer
Oct 25, 2001
165
KW
Hi there,

I have 2 arrays,

############################

where the first array:

$FirstArray[0][0] = '20050809'; //date format
$FirstArray[0][1] = 'Text1';
$FirstArray[0][2] = 'Text2';

$FirstArray[1][0] = '20050801'; //date format
$FirstArray[1][1] = 'Text7';
$FirstArray[1][2] = 'Text8';

############################

and the second array:

$SecondArray[0][0] = '20050805'; //date format
$SecondArray[0][1] = 'Text3';
$SecondArray[0][2] = 'Text4';

$SecondArray[1][0] = '20050810'; //date format
$SecondArray[1][1] = 'Text5';
$SecondArray[1][2] = 'Text6';

############################

How can I merge the 2 arrays and sort them acsending based on the date in [x][0]? so I want the result to be like the following:

############################

$FinalArray[0][0] = '20050801'; //date format
$FinalArray[0][1] = 'Text7';
$FinalArray[0][2] = 'Text8';

$FinalArray[1][0] = '20050805'; //date format
$FinalArray[1][1] = 'Text3';
$FinalArray[1][2] = 'Text4';

$FinalArray[2][0] = '20050809'; //date format
$FinalArray[2][1] = 'Text1';
$FinalArray[2][2] = 'Text2';

$FinalArray[2][0] = '20050810'; //date format
$FinalArray[2][1] = 'Text5';
$FinalArray[2][2] = 'Text6';

############################

Thanks in advance ;)
 
thanx guys,

the merge issue is solved, but the sorting issue is not!

the question now is how can I sort 2 dimensional array?

$tmpArray[0][0] = '20050809';
$tmpArray[0][1] = 'Value1';
$tmpArray[0][2] = 'Value2';

$tmpArray[1][0] = '20050803';
$tmpArray[1][1] = 'Value1';
$tmpArray[1][2] = 'Value2';

$tmpArray[2][0] = '20050805';
$tmpArray[2][1] = 'Value1';
$tmpArray[2][2] = 'Value2';

I tried all sort functions, they didn't work :(

I want to search ascending based on subset [0] which is a date. Please help ...

Thanx again in advance ;)
 
I would use usort() with a user-defined function that compares subelement 0 of each array element.

The online manual page explains this in great detail and has three sets of example code.


Want the best answers? Ask the best questions!

TANSTAAFL!!
 
If you can redesign your array, you would find your job much easier.

You currently have
Code:
$tmpArray[0][0] = '20050809';
$tmpArray[0][1] = 'Value1';
$tmpArray[0][2] = 'Value2';

$tmpArray[1][0] = '20050803';
$tmpArray[1][1] = 'Value1';
$tmpArray[1][2] = 'Value2';

$tmpArray[2][0] = '20050805';
$tmpArray[2][1] = 'Value1';
$tmpArray[2][2] = 'Value2';
If you could make your array look like
Code:
$tmpArray['20050809'][0] = 'Value1';
$tmpArray['20050809'][1] = 'Value2';

$tmpArray['20050803'][0] = 'Value1';
$tmpArray['20050803'][1] = 'Value2';

$tmpArray['20050805'][0] = 'Value1';
$tmpArray['20050805'][1] = 'Value2';

Then you should be able to use the ksort() function on the array.

Ken
 
thanx "kenrbnsn"

I prefer not to touch the code, because its too complicated :)

I'd like that if someone just took my "small" example and try to apply it and make it work for date ascending.

Please ;)
 
I tried the usort and its working :) thanx alot sleipnir214
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top