?
Solved

Sort multi-dim array

Posted on 2003-03-18
4
Medium Priority
?
216 Views
Last Modified: 2008-03-06
I have a multi-dimensional array of the following type:

$main['fname'][1] = "Bob"
$main['lname'][1] = "Dole"
$main['fname'][2] = "Bill"
$main['lname'][2] = "Clinton"
$main['fname'][3] = "George"
$main['lname'][3] = "Bush"

I'd like to sort the array values alphabetically by last name ('lname') such that the return values will be:

$main['fname'][1] = "George"
$main['lname'][1] = "Bush"
$main['fname'][2] = "Bill"
$main['lname'][2] = "Clinton"
$main['fname'][3] = "Bob"
$main['lname'][3] = "Dole"

Can anyone suggest a way to do this?

0
Comment
Question by:shaungallagher
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 1

Expert Comment

by:Thunder27
ID: 8161020
hi...  PHP's array_multisort should help

ex:

array_multisort ($main["lname"], SORT_STRING, SORT_DESC, $main["fname"]);


Note: If you have more columns, list them at the end.
0
 

Author Comment

by:shaungallagher
ID: 8161206
Yikes!  I forgot to mention that I'm only able to use PHP3, not PHP4, so array_multisort won't work for me.  Is there any PHP3 equivalent?
0
 
LVL 1

Expert Comment

by:Thunder27
ID: 8161338
Oh, bummer... I can't think of any right off, but I'll ask around
0
 
LVL 9

Accepted Solution

by:
waygood earned 315 total points
ID: 8161353
You could always write your own bubble sort.

for($x=0;$x<sizeof($main);$x++)
{
  for($y=x;$y<sizeof($main);$y++)
  {
    if($main['lname'][$y]<$main['lname'][$y+1])
    {
      $temp=$main['lname'][$y];
      $main['lname'][$y]=$main['lname'][$y+1];
      $main['lname'][$y+1]=$temp;

      $temp=$main['sname'][$y];
      $main['sname'][$y]=$main['sname'][$y+1];
      $main['sname'][$y+1]=$temp;
   
    }
  }
}

BUT if you defined your variables differently it would be easier. EG

$main[1]['fname']="esef";
$main[1]['lname']="sd";

the middle bit would just be :-

$temp=$main[$y];
$main[$y]=$main[$y+1];
$main[$y+1]=$temp;
   
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question