Created
August 9, 2017 03:23
-
-
Save TonyGao/f1739c8a88fd264da37ad0d4f657354d to your computer and use it in GitHub Desktop.
ArrClass
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Winterstrong\QQApiBundle\TypeClass; | |
class ArrClass extends ArrayObject { | |
private static $ret_obj = true; | |
public function add() { | |
$val = 0; | |
foreach($this as $vals) { | |
$val += $vals; | |
} | |
return $val; | |
} | |
public function get($i) { | |
$val = $this->offsetGet($i); | |
if(is_array($val)) { | |
return new self($val); | |
} | |
if(is_string($val) && self::$ret_obj) { | |
return new StringClass($val); | |
} | |
return $val; | |
} | |
public function each($callback) { | |
foreach($this as $key => $val) { | |
call_user_func_array($callback, array( | |
$val, $key, $this | |
)); | |
} | |
return $this; | |
} | |
public function set($i, $v) { | |
$this->offsetSet($i, $v); | |
return $this; | |
} | |
public function push($value) { | |
$this[] = $value; | |
return $this; | |
} | |
public function join($paste = '') { | |
return implode($paste, $this->getArrayCopy()); | |
} | |
public function sort() { | |
$this->asort(); | |
return $this; | |
} | |
public function toArray() { | |
return $this->getArrayCopy(); | |
} | |
public function natsort() { | |
parent::natsort(); | |
return $this; | |
} | |
public function rsort() { | |
parent::uasort('Arr::sort_alg'); | |
return $this; | |
} | |
public static function sort_alg($a,$b) { | |
if ($a == $b) { | |
return 0; | |
} | |
return ($a < $b) ? 1 : -1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment