Solved

PHP Sort Order

Posted on 2016-08-28
5
71 Views
Last Modified: 2016-08-28
Hi Experts,

I have tried many ways, but can't get this to sort by the last name.  Any suggestions?

$students_array = $this->Member->find('all', array('conditions'=>array('member_type_id'=>1), 'fields'=>array('first', 'last')), array('recursive'=>0));
        foreach($students_array as $value){
            $id = $value['Member']['id'];
            $first = $value['Member']['first'];
            $last = $value['Member']['last'];
            $students_array_formatted[$id] = "$last, $first ($id)";//formats it for the select box
        }

Open in new window

0
Comment
Question by:rcowen00
  • 3
  • 2
5 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41773842
Can you please post a sample of the contents of $students_array?  A few rows will do, thanks.

It may need to be sorted by usort(), and we will know for sure when we see the data structure.
0
 

Author Comment

by:rcowen00
ID: 41773882
array(379) {
[187]=> string(25) "Giullrida, R. (187)"
[192]=> string(22) "Siuocto, R. (192)"
[206]=> string(25) "Jeffers, J. (206)"
[209]=> string(21) "Shrad, G. (209)"
[210]=> string(21) "Shrad, J. (210)"
0
 
LVL 109

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41773929
OK, it looks like we have an array of 379 elements, and the last name is first in the elements, so a simple sort should do it.  

Here's the link to all the PHP sorting functions.
http://php.net/manual/en/array.sorting.php

Here's the function I would recommend first:
http://php.net/manual/en/function.natcasesort.php

Code (untested) is probably something like this:
$students_array = $this->Member->find('all', array('conditions'=>array('member_type_id'=>1), 'fields'=>array('first', 'last')), array('recursive'=>0));
        foreach($students_array as $value){
            $id = $value['Member']['id'];
            $first = $value['Member']['first'];
            $last = $value['Member']['last'];
            $students_array_formatted[$id] = "$last, $first ($id)";//formats it for the select box
        }

// SORT BY LAST NAME
natcasesort($students_array_formatted);

Open in new window

0
 

Author Closing Comment

by:rcowen00
ID: 41773942
Thank you!
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 41773968
Glad to help!
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now