[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Sorting an array of objects - OOP PHP

Posted on 2007-07-31
Medium Priority
Last Modified: 2008-02-01
I would like to sort an array of objects by a variable of an object variable.

I have an array of objects (obj_array).
One of the variables of (obj_a) is an object (obj_b).
I want to sort by a variable of (obj_b).

So I want to sort an
$obj_array = array (
      $butter = new obj_a,
      $flour = new obj_a,
      $water = new obj_a
by obj_a->obj_b->volume

where obj_a has a variable object called obj_b and obj_b has variable called volume
Question by:bergstrom_davin

Accepted Solution

Rurne earned 900 total points
ID: 19605290
To do that, you're going to need to define a side function for basic comparison.  With that defined, you then sort using usort().

function mySort($a, $b){
    $sub_object_a = $a->obj_b;
    $sub_object_b = $b->obj_b;

    if($sub_object_a->volume == $sub_object_b->volume){
       return 0;

    return ($sub_object_a->volume > $sub_object_b->volume) ? 1 : -1;

usort($obj_array, "mySort);

More Info:

Assisted Solution

blue_hunter earned 600 total points
ID: 19606895
see if you can arrange your array as below

$butter = new obj_a;
$flour = new obj_a;
$water = new obj_a;

$obj_array = array();
$obj_array[$butter->obj_b->volume] = $butter;
$obj_array[$flour->obj_b->volume] = $flour;
$obj_array[$water->obj_b->volume] = $water ;

$obj_array = array_values($obj_array);


Author Comment

ID: 19609311
Thank you Rurne, but I was still having trouble with sorting objects of objects by something other than volume.

Thank you blue_hunter, I liked your approach but I would lose $butter or $water if they had the same volume.

Below is a working solution I put together from both your ideas.

function sortby ($sortby = "linkid") {
  // key by sort
  foreach ($this->links as $key => $link) $sorted[$key] = $link->original->{$sortby};

  // sort by key if this sort and last sort are the same then reverse sort
  if ($sortby == $this->sortby) arsort($sorted);      // up
  else asort($sorted);                  // down

  // rekey with id
  foreach ($sorted as $key => $value) $rekeyed[$key] = $this->links[$key];
  // save sorted array
  $this->links = $rekeyed;

  // update sort for use in next pass to determine direction
  if ($sortby != "linkid") $this->sortby = $sortby;

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month18 days, 22 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question