Solved

PHP Sort Order

Posted on 2016-08-28
5
92 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
[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
  • 3
  • 2
5 Comments
 
LVL 110

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 110

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 110

Expert Comment

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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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 look for a specific file type in a local or remote server directory using PHP.

730 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