Sort Array

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
xionhack
Forum Contributor
Posts: 100
Joined: Mon Nov 10, 2008 9:22 pm

Sort Array

Post by xionhack »

Hello. I have this array:

Code: Select all

[0] => Titles Object
        (
            [id] => 1
            [dep_access_id] => 1
            [title] => Sales Administrator
            [access] => 
        )
 
    [1] => Titles Object
        (
            [id] => 2
            [dep_access_id] => 2
            [title] => Sales Consultant
            [access] => 
        )
 
    [2] => Titles Object
        (
            [id] => 3
            [dep_access_id] => 3
            [title] => Sales Receptionist
            [access] => 
        )
 
    [3] => Titles Object
        (
            [id] => 4
            [dep_access_id] => 4
            [title] => Sales BDC
            [access] => 
        )
 
    [4] => Titles Object
        (
            [id] => 5
            [dep_access_id] => 5
            [title] => Sales BDC Manager
            [access] => 
        )
 
    [5] => Titles Object
        (
            [id] => 6
            [dep_access_id] => 6
            [title] => Service BDC
            [access] => 
        )
 
    [6] => Titles Object
        (
            [id] => 7
            [dep_access_id] => 7
            [title] => Service BDC Manager
            [access] => 
        )
 
    [7] => Titles Object
        (
            [id] => 8
            [dep_access_id] => 8
            [title] => Service Manager
            [access] => 
        )
 
    [8] => Titles Object
        (
            [id] => 9
            [dep_access_id] => 9
            [title] => Service Driver
            [access] => 
        )
 
    [9] => Titles Object
        (
            [id] => 10
            [dep_access_id] => 10
            [title] => Service Assistant Manager
            [access] => 
        )
 
    [10] => Titles Object
        (
            [id] => 11
            [dep_access_id] => 11
            [title] => Parts Driver
            [access] => 
        )
 
    [11] => Titles Object
        (
            [id] => 12
            [dep_access_id] => 12
            [title] => Parts Counterman
            [access] => 
        )
 
    [12] => Titles Object
        (
            [id] => 13
            [dep_access_id] => 13
            [title] => Parts Manager
            [access] => 
        )
 
    [13] => Titles Object
        (
            [id] => 14
            [dep_access_id] => 14
            [title] => P & S Director
            [access] => 
        )
 
    [14] => Titles Object
        (
            [id] => 15
            [dep_access_id] => 15
            [title] => Shop Foreman
            [access] => 
        )
 
    [15] => Titles Object
        (
            [id] => 16
            [dep_access_id] => 16
            [title] => Finance Manager
            [access] => 
        )
 
    [16] => Titles Object
        (
            [id] => 17
            [dep_access_id] => 17
            [title] => Finance Biller
            [access] => 
        )
I want to sort it by the [title] key. How can I do that? Thanks
lunarnet76
Forum Commoner
Posts: 67
Joined: Sun Apr 04, 2010 2:07 pm
Location: Edinburgh

Re: Sort Array

Post by lunarnet76 »

hmmm not sure if it's the best way but you can simply do
<?php
$newArray=array();
foreach($yourArray as $entry){
$newArray[$entry['title']]=$entry;
}
sort($newArray);
?>
and you will have your $newArray sorted by $title
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Sort Array

Post by Jonah Bron »

You want usort() and strcmp().

Code: Select all

function my_cmp($a, $b) {
    strcmp($a['title'], $b['title']);
}
usort($yourArray, 'my_cmp');
Post Reply