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 8196823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:36:26+00:00 2026-06-07T05:36:26+00:00

I have a simple array that looks like this: array (size=6) 0 => array

  • 0

I have a simple array that looks like this:

array (size=6)
  0 => 
    array (size=2)
      'id' => int 1
      'primary' => boolean false
  1 => 
    array (size=2)
      'id' => int 2
      'primary' => boolean false
  2 => 
    array (size=2)
      'id' => int 3
      'primary' => boolean false
  3 => 
    array (size=2)
      'id' => int 4
      'primary' => boolean true
  4 => 
    array (size=2)
      'id' => int 5
      'primary' => boolean false
  5 => 
    array (size=2)
      'id' => int 6
      'primary' => boolean false

When the array is generated, it is always ordered by id, like the above. What I wish to do is to have usort() promote the array element where primary is TRUE to be the first element. There will always only be one element where primary is TRUE.

I then wrote a simple comparison function to use with usort():

$data = array(
            array('id' => 1, 'primary' => FALSE),
            array('id' => 2, 'primary' => FALSE),
            array('id' => 3, 'primary' => FALSE),
            array('id' => 4, 'primary' => TRUE),
            array('id' => 5, 'primary' => FALSE),
            array('id' => 6, 'primary' => FALSE),
        );

function sortArray($a, $b){

    if($a['primary']){
        return -1;
    }elseif($b['primary']){
        return 1;
    }else{
        return 0;
    }
}

var_dump($data);

usort($data, 'sortArray');

var_dump($data);

While the element with primary = TRUE is now promoted to the front, the rest of the elements are now sorted in reverse order and the element with id = 5 is out of place:

array (size=6)
  0 => 
    array (size=2)
      'id' => int 4
      'primary' => boolean true
  1 => 
    array (size=2)
      'id' => int 5
      'primary' => boolean false
  2 => 
    array (size=2)
      'id' => int 6
      'primary' => boolean false
  3 => 
    array (size=2)
      'id' => int 3
      'primary' => boolean false
  4 => 
    array (size=2)
      'id' => int 2
      'primary' => boolean false
  5 => 
    array (size=2)
      'id' => int 1
      'primary' => boolean false

I know I can compare the ids, if both elements have the primary property as false:

function sortArray($a, $b){

    if($a['primary']){
        return -1;
    }elseif($b['primary']){
        return 1;
    }else{
        return $a['id'] > $b['id'];
    }
}

But, why is usort randomizing the order of my other elements? Is it possible to use usort() to just promote elements and leave the rest of the array untouched?

  • 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-07T05:36:27+00:00Added an answer on June 7, 2026 at 5:36 am

    Try this comparison function:

    function sortArray($a, $b)
    {
        if($a['primary'])
        {
            $result = 1;
        }
        elseif($b['primary'])
        {
            $result = -1;
        }
        else
        {
            $result = ($a['id'] < $b['id']) ? -1 : 1;
        }
    
        return $result;
    }
    

    You can change $result = ($a['id'] < $b['id']) ? -1 : 1; to $result = ($a['id'] < $b['id']) ? 1 : -1; to sort in reverse order.

    In your comparison function, return $a['id'] > $b['id'] will either return 0 or 1 (true or false), never -1. From the documentation:

    “The comparison function must return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second.”

    The fastest comparison sort is O(N*log(N)), which is slower than linear time. For this reason, I recommend just iterating over the array (O(N), linear time) and moving the element with ‘primary’ set to true to the front of the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple recursive array function that looks like this: function recursive_array($results) {
I have a simple PHP Array called $categories that looks like this: Array (
I have an array that looks like this (sample): Array ( [1600] => Array
I have a simple Ruby script that looks like this require 'csv' while line
I have a bool array that looks like this bool[] responses = new bool[]
I have a pretty simple structure that looks something like this: var list =
I have declared a DLL import in my C# program that looks like this:
I have some javascript that looks like this: var sel = '<option value={value} {selected}>{name}</option>';
I have Django model that looks like this: class Categories(models.Model): Model for storing the
I have a url that looks like this: http://localhost/store/mens/category/t-shirts/item/a-t-shirt I have a class called

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.