Current location: Hot Scripts Forums » Programming Languages » PHP » Multidimensional Array Sorting


Multidimensional Array Sorting

Reply
  #1 (permalink)  
Old 08-23-03, 12:10 PM
Kage Kage is offline
New Member
 
Join Date: Aug 2003
Location: Destination
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Question Multidimensional Array Sorting

Currently, I have arrays like this:

Array (
[0] => Array (
[id] => 1,
[name] => "name",
[login] => "123456789"
),
[1] => Array (
[id] => 2,
[name] => "foo",
[login] => "987654321"
),
[2] => Array (
[id] => 3,
[name] => "bar",
[login] => "123454321"
)
)

and so on. What I want to do is arrange the first dimension-array by a key I specify that would be in the second dimension-array. For example, if I wanted it to sort by 'name,' it [the first dimension] would come out as 2, 1, 0, but the order of things in each second dimension would remain the same (id, name, login).

I've tried a lot of different ways to do this, and my last resort would be to code out 50 or so lines of breaking it down and sorting it manually, but I know this can be done by PHP. Does anyone know how?
__________________
“A script is what you give the actors. A program is what you give the audience.”
~Larry Wall - Perl Creator
Reply With Quote
  #2 (permalink)  
Old 08-24-03, 09:26 PM
ermau's Avatar
ermau ermau is offline
Wannabe Coder
 
Join Date: Aug 2003
Location: Florida, USA
Posts: 240
Thanks: 0
Thanked 0 Times in 0 Posts
Are you saying you want something like this?

Array (
[0] => Array (
[id] => 3,
[name] => "bar",
[login] => "123454321"
),
[1] => Array (
[id] => 1,
[name] => "name",
[login] => "123456789"
),
[2] =>Array (
[id] => 2,
[name] => "foo",
[login] => "987654321"
)
)
__________________
PHP / mySQL Developer
Reply With Quote
  #3 (permalink)  
Old 06-15-05, 04:56 PM
JBChris JBChris is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Exclamation

Bump...

I would like it if this question could be answered... I need it. I'm trying to sort a mysql result array by the zip code. If someone could answer this, I would be most grateful.

Thanks,
Josh
Reply With Quote
  #4 (permalink)  
Old 06-15-05, 11:49 PM
jordie jordie is offline
Wannabe Coder
 
Join Date: Jun 2005
Posts: 179
Thanks: 0
Thanked 0 Times in 0 Posts
I'll work on it and get back to u soon.
__________________
Jordie Bodlay
php, mysql, postgres
css, xhtml
graphics, design
email me: jb2386@hotmail.com for any programming needs.
Reply With Quote
  #5 (permalink)  
Old 06-16-05, 12:04 AM
jordie jordie is offline
Wannabe Coder
 
Join Date: Jun 2005
Posts: 179
Thanks: 0
Thanked 0 Times in 0 Posts
Here you go:

PHP Code:

$myRay = Array (

'0' => Array (
'id' => 1,
'name' => "name",
'login' => "123456789"
),
'1' => Array (
'id' => 2,
'name' => "foo",
'login' => "987654321"
),
'2' => Array (
'id' => 3,
'name' => "bar",
'login' => "123454321"
)
);



function 
namecmp$row1,$row2 )
{
    return 
strcmp($row1['login'], $row2['login']) ;
   
}
uasort($myRay,'namecmp');

echo 
"<pre>" ;
print_r$myRay ) ;
echo 
"</pre>" 
__________________
Jordie Bodlay
php, mysql, postgres
css, xhtml
graphics, design
email me: jb2386@hotmail.com for any programming needs.
Reply With Quote
  #6 (permalink)  
Old 06-16-05, 10:46 AM
JBChris JBChris is offline
Newbie Coder
 
Join Date: Sep 2004
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you very much! That was exactly what I needed!
Reply With Quote
  #7 (permalink)  
Old 12-12-05, 04:19 PM
dave111 dave111 is offline
Wannabe Coder
 
Join Date: Jul 2003
Posts: 136
Thanks: 0
Thanked 0 Times in 0 Posts
Is it possible to make this function descending instead of ascending order?

Code:
function namecmp( $row1,$row2 )
{
    return strcmp($row1['login'], $row2['login']) ;
   
}
uasort($myRay,'namecmp');
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
move array element up or down Perry JavaScript 4 09-27-03 04:23 PM
passing array to hidden fields in form on next page? seala ASP 2 09-04-03 02:40 PM
asp: values in array not in order?? seala ASP 0 08-16-03 12:06 PM
call, array and display? irfaan PHP 5 08-09-03 05:41 PM
Moving array index/values around Cagez PHP 1 07-15-03 02:34 AM


All times are GMT -5. The time now is 05:34 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.