$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 { }