Sort an array by object attribute in PHP

One of those simple things that I keep forgetting.

Suppose you have to sort a set of PHP objects, based on an attribute.

To do this, you can implement the __toString method and use the SORT_STRING flag in the sort function:

<?php

class Car {
    private $_maker;

    public function __construct( $maker ) {
        $this->maker = $maker;
    }


    public function __toString() {
        return $this->maker;
    }
}

$car1 = new Car( 'BMW' );
$car2 = new Car( 'Alfa Romeo' );
$car3 = new Car( 'Volvo' );

$cars   = [];
$cars[] = $car1;
$cars[] = $car2;
$cars[] = $car3;

// sort the array
sort( $cars, SORT_STRING );

var_dump( $cars );

This will result in:

/*
==RESULT== 
array(3) {
  [0]=>
  object(Car)#2 (2) {
    ["_maker":"Car":private]=>
    NULL
    ["maker"]=>
    string(10) "Alfa Romeo"
  }
  [1]=>
  object(Car)#1 (2) {
    ["_maker":"Car":private]=>
    NULL
    ["maker"]=>
    string(3) "BMW"
  }
  [2]=>
  object(Car)#3 (2) {
    ["_maker":"Car":private]=>
    NULL
    ["maker"]=>
    string(5) "Volvo"
  }
}
 */

Posted

in

by

Tags:

Comments

Leave a Reply

%d bloggers like this: