[PHP] Sorting Multi-Dimensional Arrays using uksort()

2002-04-01 Thread Bloom, Chris

Hey folks: 

I have a function that returns an array. I'm assigning the returned array to
a variable inside another function: 
-- 
$dirs = getFileArray($parentDir,title_2.txt); 
-- 

If I use print_r() on the variable it writes out as expected: 
-- 
Array 
( 
[0] = Array 
( 
[folderName] = 97StudioPhotos 
[linkText] = Studio 3/27 - 3/30 (by Matt Myrdal/VE) 
[sortDate] = 03/31/02 

) 

[1] = Array 
( 
[folderName] = 98PromoPhotos 
[linkText] = Promo Photos BW (by Matt Myrdal - 3/24/02) 
[sortDate] = 03/24/02 

) 

[2] = Array 
( 
[folderName] = 99PhotoSet1 
[linkText] = Photo Set I (by Matt Myrdal - 3/12/02) 
[sortDate] = 03/12/02 

) 

) 
-- 

Now I want to sort this array using a custom defined sort method: 
-- 
uksort($dirs,sortByDateDesc); 

function sortByDateDesc($a,$b) 
{ 
print hra = br; 
print_r($a); 
print hr; 

print hrb = br; 
print_r($b); 
print hr; 

print $a. vs .$b.br; 

if ($a[sortDate]  $b[sortDate]){ 
print  .$a[sortDate].  .$b[sortDate].br; 
return -1; 
} else if ($a[sortDate]  $b[sortDate]){ 
print  .$a[sortDate].  .$b[sortDate].br; 
return 1; 
} else { 
print  .$a[sortDate]. = .$b[sortDate].br; 
return 0; 
} 
} 
-- 

As you can see, I've added a bunch of print() calls for testing. The trouble
is that none of the variables are being output into the print functions and
all of the print_r() calls just return 1. In the end, the array is not
being sorted at all. Why can't I access any of the array elements in this
function and why won't it sort correctly? 

I've tried accessing the elements everyway I can think of: 
$a[sortDate] 
$a[sortDate] 
$a[2] 

Regards, 
Chris Bloom
PS: Attached is an example file
 arr_sort_test.php 
Web Infrastructure Analyst
Lifespan Corporate Services
167 Point Street #245
Providence, RI 02903
401-444-2074




-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


RE: [PHP] Sorting Multi-Dimensional Arrays using uksort()

2002-04-01 Thread Bloom, Chris

Sweet!  Thanks for the help!

-Original Message-
From: Martin Towell [mailto:[EMAIL PROTECTED]]
Sent: Monday, April 01, 2002 7:15 PM
To: 'Bloom, Chris'; '[EMAIL PROTECTED]'
Subject: RE: [PHP] Sorting Multi-Dimensional Arrays using uksort()


try using usort() instead of uksort() - (i haven't checked the manual but) I
believe uksort is for sorting based on the keys and not the values, which is
what you're trying to do

-Original Message-
From: Bloom, Chris [mailto:[EMAIL PROTECTED]]
Sent: Tuesday, April 02, 2002 9:57 AM
To: '[EMAIL PROTECTED]'
Cc: Bloom, Chris
Subject: [PHP] Sorting Multi-Dimensional Arrays using uksort()


Hey folks: 

I have a function that returns an array. I'm assigning the returned array to
a variable inside another function: 
-- 
$dirs = getFileArray($parentDir,title_2.txt); 
-- 

If I use print_r() on the variable it writes out as expected: 
-- 
Array 
( 
[0] = Array 
( 
[folderName] = 97StudioPhotos 
[linkText] = Studio 3/27 - 3/30 (by Matt Myrdal/VE) 
[sortDate] = 03/31/02 

) 

[1] = Array 
( 
[folderName] = 98PromoPhotos 
[linkText] = Promo Photos BW (by Matt Myrdal - 3/24/02) 
[sortDate] = 03/24/02 

) 

[2] = Array 
( 
[folderName] = 99PhotoSet1 
[linkText] = Photo Set I (by Matt Myrdal - 3/12/02) 
[sortDate] = 03/12/02 

) 

) 
-- 

Now I want to sort this array using a custom defined sort method: 
-- 
uksort($dirs,sortByDateDesc); 

function sortByDateDesc($a,$b) 
{ 
print hra = br; 
print_r($a); 
print hr; 

print hrb = br; 
print_r($b); 
print hr; 

print $a. vs .$b.br; 

if ($a[sortDate]  $b[sortDate]){ 
print  .$a[sortDate].  .$b[sortDate].br; 
return -1; 
} else if ($a[sortDate]  $b[sortDate]){ 
print  .$a[sortDate].  .$b[sortDate].br; 
return 1; 
} else { 
print  .$a[sortDate]. = .$b[sortDate].br; 
return 0; 
} 
} 
-- 

As you can see, I've added a bunch of print() calls for testing. The trouble
is that none of the variables are being output into the print functions and
all of the print_r() calls just return 1. In the end, the array is not
being sorted at all. Why can't I access any of the array elements in this
function and why won't it sort correctly? 

I've tried accessing the elements everyway I can think of: 
$a[sortDate] 
$a[sortDate] 
$a[2] 

Regards, 
Chris Bloom
PS: Attached is an example file
 arr_sort_test.php 
Web Infrastructure Analyst
Lifespan Corporate Services
167 Point Street #245
Providence, RI 02903
401-444-2074


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php