0

I've seen similar questions, I simply do not understand the answers. Hopefully if I ask specifically about my code something will click. I've been stuck for some time and would really appreciate the help.

I have an array: foreach ($showresult as $display) { $display_result[] = array ('parkid' =>$display['parkid'],

                 'trailsys' =>$display['trailsys'],

                         'trailset' =>$display['trailset'],

                         'name' =>$display['name'], 

                         'description' =>$display['description'], 

                         'url' =>$display['url'], 

                                 'ldes' =>$display['ldes'],

                                 'ltxt' =>$display['ltxt'],

                         'address' =>$display['address'], 

                         'city' =>$display['city'], 

                         'zip' =>$display['zip'], 

                         'phone' =>$display['phone'], 

                         'pos' =>$display['pos'],   

                         'T1' => $display['systemname'],

                 'T2'=> $display['name']);

 {                           'state' =>$display['state'], 

Every item in the array has a value in T1. T2 can have a value, or it can be 0. I want to alphabetize the list by T1. In some cases, there will be three or four entries where T1 is the same, then I want to alphabetize those by T2. I'm stumped.

Please help.

user1483042
  • 131
  • 3
  • 14
  • possible duplicate of [How do I Sort a Multidimensional Array in PHP](http://stackoverflow.com/questions/96759/how-do-i-sort-a-multidimensional-array-in-php) – Jon Mar 02 '14 at 23:37

1 Answers1

0

You should try with usort() function ( http://www.php.net/usort ). It allows you to sort array by custom function. So, in your case it would be something like:

<?
  usort($array,function($a,$b) {
    if ($a['T1'] < $b['T1']) return -1;
    elseif ($a['T1'] > $b['T1']) return 1;
    else {
      if ($a['T2'] < $b['T2']) return -1;
      elseif ($a['T2'] > $b['T2']) return 1;
      else return 0;
    }
  });
?>

You could check it out and adjust to your needs.

Let me know if this works for you.

Akshay Kalose
  • 789
  • 1
  • 5
  • 14
dkasipovic
  • 5,486
  • 16
  • 24
  • Okay, Here's where I feel stupid. What do $a and $b represent? – user1483042 Mar 02 '14 at 23:43
  • 1
    Well they represent two elements of the `$array`. That's how `usort()` works. – dkasipovic Mar 02 '14 at 23:44
  • $a and $b are each item in the array. – Akshay Kalose Mar 02 '14 at 23:44
  • I get the following error: Warning: usort() expects parameter 1 to be array, null given in /home/ride4w5/public_html/test_site/test.php on line 59 – user1483042 Mar 02 '14 at 23:45
  • Well your `$array` is null. What is the variable holding your array? – dkasipovic Mar 02 '14 at 23:46
  • Is your array variable called $array? – Akshay Kalose Mar 02 '14 at 23:46
  • Ha! That's why I feel stupid. I changed it to the correct array name and it works. I still have so much to learn. Thank you so much for your help. – user1483042 Mar 02 '14 at 23:49
  • I am glad I can be of help. – dkasipovic Mar 02 '14 at 23:50
  • Wait, that's close. This sorts alphabetically by T1, or T2 if there is a value. What I want is for it to alphabetize by T1 ALWAYS but if there is more than one entry with the same T1 value, then to alphabetize those by T2. – user1483042 Mar 03 '14 at 00:13
  • This sorts by T2 if T1 are equal. You could either, sort once by T1, then sort some of the elements again by T2. Or you could create an array, like `$ts = Array()` and then populate it with `T1` in the `usort()` function. Then you check if `T1` exists in `$ts` and sort by `T2`, else add it to array and sort by `T1`. – dkasipovic Mar 03 '14 at 08:29