Jump to content

Sort Array


xionhack

Recommended Posts

Hello. I have this array:

 

[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

 

 

Link to comment
Share on other sites

For multi-dimensional arrays I prefer to use usort() over array_multisort() because array_multisort() requires you to recreate the array as multiple arrays in order to work. usort() allows you to create a function to be as simple or complex as you need. In this case:

 

function sortTitles($a, $b)
{
  if($a['title']==$b['title']) { return 0; }
  return ($a['title'] > $b['title']) ? 1 : -1;
}

usort($theArray, 'sortTitles');

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.