开发者

Custom array sorting based on instance properties

开发者 https://www.devze.com 2022-12-24 04:33 出处:网络
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.

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

精彩评论

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

关注公众号