?
Solved

PHP Sort Order

Posted on 2016-08-28
5
Medium Priority
?
121 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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

Industry Leaders: 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

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

777 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