123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336 |
- <?php
- namespace loyalsoft;
- /**
- * Description of StlUtil
- *
- * @author jgao
- */
- class StlUtil {
- //put your code here
- /**
- * 字典是否存在某元素
- * @param associat_array $dict
- * @param type $property
- */
- public static function dictHasProperty($dict, $property) {
- return property_exists($dict, "" . $property);
- }
- /**
- * 移除字典元素
- * @param associat_array $dict
- * @param type $property
- */
- public static function dictRemove($dict, $property) {
- unset($dict->$property);
- }
- /**
- * 获取字典全部属性
- * @param associat_array $dict
- * @return array
- */
- public static function dictPropertys($dict) {
- $propertys = array();
- foreach ($dict as $key => $val) {
- $propertys[] = $key;
- }
- return $propertys;
- }
- /**
- * 随机字典任意属性名
- * @param associat_array $dict
- */
- public static function dictPropertyRandom($dict) {
- return self::arrayRandom(self::dictPropertys($dict));
- }
- /**
- * 随机字典任一属性值
- * @param type $dict
- * @return obj
- */
- public static function dictRandom($dict) {
- $key = self::arrayRandom(self::dictPropertys($dict));
- return $dict->$key;
- }
- /**
- * 字典转数组
- * @param associat_array $dict 目标字典
- * @param type $mod 0.值转换;1.键转换;2.部分属性转换
- * @param type $propertyList 属性列表【数组】,仅$mod==2时生效
- * @return type
- */
- public static function dictToArray($dict, $mod = 0, $propertyList = null) {
- $array = array();
- foreach ($dict as $key => $val) {
- if ($mod == 0) { // 值转数组
- $array[] = $val;
- } elseif ($mod == 1) { // key转数组
- $array[] = $key;
- } else { // 单属性转数组
- $item = new \stdClass();
- foreach ($propertyList as $property) {
- if (self::dictHasProperty($dict, $property)) {
- $item->$property = $dict->$property;
- }
- }
- $array[] = $item;
- }
- }
- return $array;
- }
- /**
- * 创建数组,并将传入的参数压入数组
- * @param 可选...args
- */
- public static function arrayInit(/* $arg1, $arg2...$argN */) {
- $array = array();
- $items = func_get_args();
- foreach ($items as $item) {
- $array[] = $item;
- }
- return $array;
- }
- /**
- * 数组新增
- * @param array $array
- * @param type $item
- */
- public static function arrayPush(&$array, $item) {
- $array[] = $item;
- }
- /**
- * 获取数组元素
- * @param type $array
- * @param type $index
- */
- public static function arrayGet(&$array, $index) {
- return $array[$index];
- }
- /**
- * 设置数组元素
- * @param type $array
- * @param type $index
- * @param type $item
- * @return type
- */
- public static function arraySet(&$array, $index, $item) {
- if ($index >= count($array)) {
- $array[] = $item;
- return;
- }
- $array[$index] = $item;
- }
- /**
- * 数组插入
- * @param type $array
- * @param type $index
- * @param type $item
- */
- public static function arrayInsert(&$array, $index, $item) {
- $fore = ($index == 0) ? array() : array_splice($array, 0, $index);
- $fore[] = $item;
- $array = array_merge($fore, $array);
- }
- /**
- * 数组移除
- * @param array $array 数组
- * @param any $item 元素
- */
- public static function arrayRemove(&$array, $item) {
- $index = self::arrayIndexOf($array, $item);
- if ($index == -1) {
-
- } else {
- array_splice($array, $index, 1);
- }
- }
- // /**
- // * 数组移除
- // * @param array $array
- // * @param int $index
- // */
- // public static function arrayRemoveAll(&$array, $item) {
- // $index = self::arrayIndexOf($array, $item);
- // if ($index == -1) {
- //
- // } else {
- // array_splice($array, $index, 1);
- // }
- // }
- /**
- * 数组移除
- * @param array $array
- * @param int $index
- */
- public static function arrayRemoveAt(&$array, $index) {
- array_splice($array, $index, 1);
- }
- /**
- * 数组克隆
- * @param array $array
- */
- public static function arrayClone($array) {
- return array_slice($array, 0);
- }
- /**
- * 数组清空
- * @param array $array
- */
- public static function arrayClear(&$array) {
- array_splice($array, 0);
- }
- /**
- * 元素是否位于某数组中
- * @param array $array
- * @param any $item
- * @return type
- */
- public static function arrayContains($array, $item) {
- return in_array($item, $array);
- }
- /**
- * 查找元素在数组中的位置
- * @param array $array
- * @param any $item
- * @return int
- */
- public static function arrayIndexOf($array, $item) {
- $indexs = array_keys($array, $item);
- if (count($indexs) > 0) {
- return $indexs[0];
- } else {
- return -1;
- }
- }
- /**
- * 数组去重
- * @param type $array
- */
- public static function arrayUnique(&$array) {
- $array = array_values(array_unique($array));
- }
- /**
- * 模仿as3中的sortby函数
- * @param type $arr
- * @param type $propertyName_sortBy
- */
- public static function arraySortBy($arr, $propertyName_sortBy) {
- throw new ErrorException("funciton not finish!");
- $a = function ($a, $b) {
- if ($a->id > $b->id) {
- return 1;
- } elseif ($a->id == $b->id) {
- return 0;
- } else {
- return -1;
- }
- };
- }
- /**
- * 在数组中查找第一个符合条件的值
- * @param mixed $array
- * @param bool $closure 是否符合条件的判断方法
- * @return type
- */
- public static function arrayFind($array, $closure) {
- foreach ($array as $key => $value) {
- if ($closure($value, $key)) {
- return $value;
- }
- }
- return null;
- }
- /**
- * 辅助方法,将关联数组转换成class
- * @param type $assocArray
- * @return \stdClass
- */
- public static function array2class($assocArray) {
- $ret = new \stdClass();
- foreach ($assocArray as $p => $v) {
- $ret->$p = $v;
- }
- return $ret;
- }
- /**
- * 辅助方法,将数组转换成对象结构方便使用箭头操作
- * @param array $arr [{'kname':vallue,some:value},{'kname':vallue,some:value}...]
- * @param string $kName 作为key的字段的名称
- * @return \stdClass
- */
- public static function array2dict($arr, $kName) {
- $ret = new \stdClass();
- foreach ($arr as $item) {
- $k = $item->$kName;
- if ($k) {
- $ret->$k = $item;
- }
- }
- return $ret;
- }
- /**
- * 统计数组元素数或者对象属性个数
- * @param type $var
- * @return type
- */
- public static function count($var) {
- return count((array) $var);
- }
- /**
- * 确保对象是数组
- * @param type $mix
- * @return type
- */
- public static function ArrayEnsure($mix) {
- if (is_array($mix)) {
- return $mix;
- }
- return (array) $mix;
- }
- }
- /**
- * 函数式写法
- * @param type $arr
- * @param type $item
- * @return type
- */
- function array_contains($arr, $item) {
- return StlUtil::arrayContains($arr, $item);
- }
- //\
- /**
- * 关联数组, json_encode之后转为{}
- */
- class asoc_array {
-
- }
|