Solved

PHP Sort Order

Posted on 2016-08-28
5
77 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Recently I spent hours debugging an issue in a Rails project where ActiveRecord was causing MySQL errors trying to create a User object of a class at the top level of a Single Table Inheritance model structure.  It turns out `.create` behaves differ…
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…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

860 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