Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8718817
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:38:23+00:00 2026-06-13T06:38:23+00:00

PHP Multidimensional array custom sort. Sort should be based on values in field [position]

  • 0

PHP Multidimensional array custom sort.

Sort should be based on values in field [position]
A person can have multiple positions (see the special case listed below).

Array
(
    [0] => Array
    (
        [position] => Array
            (
                [0] => Secretary
            )
    )
)

I want the array to be ordered on position values as

  1. General Secretary
  2. President
  3. Treasurer
  4. Secretary
  5. Committee Member

Special case
for the below array, it should be the first array. Because this person is “General Secretary”

[2] => Array
(
    [person_id] => 51136
    [position] => Array
        (
            [0] => General Secretary
            [1] => Committee Member
        )

    [person_name] => Person 1
)

Example input data

Array
(
    [0] => Array
    (
        [person_id] => 22180
        [position] => Array
            (
                [0] => Secretary
            )
        [person_name] => Person 1
    )

    [1] => Array
    (
        [person_id] => 51135
        [position] => Array
            (
                [0] => President
            )
        [person_name] => Person 2
    )

    [2] => Array
    (
        [person_id] => 51136
        [position] => Array
            (
                [0] => General Secretary
                [1] => Committee Member
            )

        [person_name] => Person 3
    )

    [3] => Array
    (
        [person_id] => 44141
        [position] => Array
            (
                [0] => Treasurer 
            )

        [person_name] => Person 4
    )

    [4] => Array
    (
        [person_id] => 51137
        [position] => Array
            (
                [0] => Committee Member
            )
        [person_name] => Person 5
    )
)

Output required

Array
(
    [0] => Array
    (
        [person_id] => 51136
        [position] => Array
            (
                [0] => General Secretary
                [1] => Committee Member
            )

        [person_name] => Person 3
    )
    [1] => Array
    (
        [person_id] => 51135
        [position] => Array
            (
                [0] => President
            )
        [person_name] => Person 2
    )
    [2] => Array
    (
        [person_id] => 44141
        [position] => Array
            (
                [0] => Treasurer 
            )

        [person_name] => Person 4
    )
    [3] => Array
    (
        [person_id] => 22180
        [position] => Array
            (
                [0] => Secretary
            )
        [person_name] => Person 1
    )
    [4] => Array
    (
        [person_id] => 51137
        [position] => Array
            (
                [0] => Committee Member
            )
        [person_name] => Person 5
    )
)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:38:24+00:00Added an answer on June 13, 2026 at 6:38 am

    Alright, I make this out.

    You can still use usort() instead of re-inventing the wheel. Just compare two person by the position like array_search($position,$pos_array).

    function sortByPosition($ps)
    {
        $pos=array(0=>"General Secretary",1=>"President",2=>"Treasurer",3=>"Secretary",4=>"Committee Member");
        usort($ps,function($p1,$p2) use ($pos){
            $lvl1=count($pos);
            $lvl2=count($pos);
            foreach($p1["position"] as $position)
            {
                $lvl1=min(array_search($position,$pos),$lvl1);
            }
            foreach($p2["position"] as $position)
            {
                $lvl2=min(array_search($position,$pos),$lvl2);
            }
            return $lvl1-$lvl2;
        });
        return $ps;
    }
    

    Test:

    $arr=Array(Array("person_id" => 22180,"position" => Array("Secretary"),"person_name" => "Person 1"), Array("person_id" => 51135,"position" => Array("President"),"person_name" => "Person 2"), Array("person_id" => 51136,"position" => Array("General Secretary","Committee Member"),"person_name" => "Person 3"), Array("person_id" => 44141,"position" => Array("Treasurer"),"person_name" => "Person 4"), Array("person_id" => 51137,"position" => Array("Committee Member"),"person_name" => "Person 5"));
    print_r(sortByPosition($arr));
    

    Outputs:

    Array
    (
        [0] => Array
            (
                [person_id] => 51136
                [position] => Array
                    (
                        [0] => General Secretary
                        [1] => Committee Member
                    )
    
                [person_name] => Person 3
            )
    
        [1] => Array
            (
                [person_id] => 51135
                [position] => Array
                    (
                        [0] => President
                    )
    
                [person_name] => Person 2
            )
    
        [2] => Array
            (
                [person_id] => 44141
                [position] => Array
                    (
                        [0] => Treasurer
                    )
    
                [person_name] => Person 4
            )
    
        [3] => Array
            (
                [person_id] => 22180
                [position] => Array
                    (
                        [0] => Secretary
                    )
    
                [person_name] => Person 1
            )
    
        [4] => Array
            (
                [person_id] => 51137
                [position] => Array
                    (
                        [0] => Committee Member
                    )
    
                [person_name] => Person 5
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: php sort array by sub-value I have a multidimensional array like the
I have a php multidimensional array (2 levels) with some values and I want
Possible Duplicate: How do I sort a multidimensional array in php I have value
Possible Duplicate: PHP Sort a multidimensional array by element containing date I have some
Possible Duplicate: How do I sort a multidimensional array in php I have multidimensional
Possible Duplicate: Sorting multidimensional array in PHP How can I sort by key in
hi i have multidimensional array in php i want to remove an index from
How can I return multidimensional array keys in tree format in PHP? For example,
I have a PHP class that stores a complex multidimensional array, and rather than
I have a multidimensional array in PHP produced by the great examples of icio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.