Solved

PHP Sort Order

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

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
Thank you!
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
Glad to help!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
How to keep a password file on your Windows PC? 21 155
mysql Encryption with PHP 8 45
Phone Dialer 5 35
Wordpress Body Class 5 7
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

762 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

8 Experts available now in Live!

Get 1:1 Help Now