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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:43:09+00:00 2026-06-09T10:43:09+00:00

say i have an array of objects: Array ( [0] => stdClass Object (

  • 0

say i have an array of objects:

Array (
  [0] => stdClass Object (
    [id] => 1
    [name] => product_1
    [cost] =>9.99
  )
  [1] => stdClass Object (
    [id] => 2
    [name] => product_2
    [cost] =>2.99
  )
  [2] => stdClass Object (
    [id] => 3
    [name] => product_3
    [cost] =>4.99
  )
  [3] => stdClass Object (
    [id] => 4
    [name] => product_4
    [cost] =>1.99
  )
  [4] => stdClass Object (
    [id] => 5
    [name] => product_5
    [cost] =>0.99
  )
)

I want to order them starting from the lowest cost to the highest however the first element in the array has to have the [name] of “product_3”.

  • 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-09T10:43:10+00:00Added an answer on June 9, 2026 at 10:43 am

    To do this, you can’t rely on sorting alone. You’d need a bit more logic:

    1. Capture the soon-to-be first element in its own variable.
    2. Remove that element from the array.
    3. Sort the array using usort().
    4. Shift the element from #1 to the front of the array with array_unshift().

    So, for #1, you can do a bunch of things. The simplest is to loop the array to find the key that indexes where the first object is, and unset() it:

    $first = null;
    foreach( $array as $key => $obj) {
        if( $obj->name == 'product_3') {
            $first = $obj;
            unset( $array[ $key ]); 
            break;
        }
    }
    

    Now you have the first element in $first, so you have to sort the array with usort():

    usort( $array, function( $a, $b) {  
        if( $a->cost == $b->cost) 
            return 0; 
        return $a->cost < $b->cost ? 1 : -1; // Might need to switch 1 and -1
    });
    

    Finally, add the first element back to the beginning of the now-sorted array:

    array_unshift( $array, $first);
    

    Disclaimer: None of the above implementation was tested.

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

Sidebar

Related Questions

Let's say I have an array list of objects, and each object has the
Say I have an array of objects similar to [ {name:value1, action:U}, {name:text2, action:d}
I have array of Objects (let say that class name is Snap) Snap. Is
Say I have an array of objects that I want to be able to
I have an array with objects 1-10 (let's say). I want the cell.textLabel.text =
Say I have a Form object which has an array of Tab objects. var
Let's say I have an array of objects [{href: 'some_uri', 'class': 'this-css-class'}, {href: 'another_uri',
Let's say I have an array of primitives or a list of objects, doesn't
I have a method which returns an array of fixed type objects (let's say
Say I have an object-like data record like this: $article = array( 'title' =>

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.