tags:

views:

28

answers:

1

I'm trying to perform a usort on an array inside an instance of a class. But the sort is dependent on the properties of said instance.

Code (which doesn't work):

class foo {
  private $array;
  private $key;
  private $dir;

  function sort() {
    usort($this->array, array("foo", "orderArray"));
  }

  function orderArray($a, $b) {
    return strcmp($a[$this->key], $b[$this->key]) * $this->dir;
  }
}

From the orderArray class, you can't access $key or $dir. The question is, how can I write this so I can?

+3  A: 

It looks like you just want to see the current instance? Pass it in the callback.

  function sort() {
    usort($this->array, array($this, "orderArray"));
  }
Joseph Mastey
**facepalm** Thank you.
St. John Johnson