开发者

Custom array sorting based on instance properties

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开发者_如何学JAVA I can?


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜