$property); } /** * 添加字典元素 * @param type $dict * @param type $property * @param type $value */ static function dictInsert($dict, $property, $value){ $dict->$property = $value; } /** * 获取字典值 * @param type $dict * @param type $property */ static function dictValue($dict, $property, $default=null){ if(property_exists($dict, "".$property)){ return $dict->$property; }else{ return $default; } } /** * 获取字典全部属性 * @param type $dict * @return type */ static function dictPropertys($dict){ $propertys = array(); foreach($dict as $key => $val){ $propertys[] = $key; } return $propertys; } /** * 获取字典全部值 * @param type $dict */ static function dictValues($dict){ $values = array(); foreach ($dict as $key => $val){ $values[] = $val; } return $values; } /** * 随机字典任意属性名 * @param type $dict */ static function dictRandom($dict){ $kvs = array(); $key = self::arrayRandom(self::dictPropertys($dict)); $kvs[$key] = $dict->$key; return $kvs; } /** * 随机字典任一属性值 * @param type $dict * @return type */ static function dictRandoms($dict, $num){ $kvs = array(); $keys = self::arrayRandoms(self::dictPropertys($dict),$num); foreach ($keys as $key){ $kvs[$key] = $dict->$key; } return $kvs; } /** * 字典转数组 * @param type $dict 目标字典 * @param type $mod 0.值转换;1.键转换;2.部分属性转换 * @param type $propertyList 属性列表【数组】,仅$mod==2时生效 * @return type */ static function dictToArray($dict, $mod=0, $propertyList=null){ $array = array(); foreach($dict as $key=>$val){ if($mod == 0){ // 值转数组 $array[] = $val; }else if($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 array $array * @param type $item */ static function arrayPush(&$array,$item){ $array[] = $item; } /** * 获取数组元素 * @param type $array * @param type $index */ static function arrayGet(&$array, $index){ return $array[$index]; } /** * 设置数组元素 * @param type $array * @param type $index * @param type $item * @return type */ static function arraySet(&$array, $index, $item){ if($index >= count($array)){ $array[] = $item; return; } $array[$index] = $item; } /** * 数组插入 * @param type $array * @param type $item * @param type $index */ static function arrayInsert(&$array,$index,$item){ if($index >= count($array)){ $array[] = $item; return; } $fore = ($index==0)?array():array_splice($array,0,$index); $fore[] = $item; $array = array_merge($fore,$array); } /** * 数组移除 * @param type $array * @param type $item */ static function arrayRemove(&$array,$item){ array_splice($array,self::arrayIndexOf($array,$item),1); } /** * 数组移除 * @param type $array * @param type $index */ static function arrayRemoveAt(&$array,$index){ array_splice($array,$index,1); } /** * 数组克隆 * @param type $array */ static function arrayClone($array){ return array_slice($array, 0); } /** * 数组清空 * @param type $array */ static function arrayClear(&$array){ array_splice($array,0); } /** * 元素是否位于某数组中 * @param type $array * @param type $item * @return type */ static function arrayContains($array,$item){ return in_array($item, $array); } /** * 查找元素在数组中的位置 * @param type $array * @param type $item * @return type */ static function arrayIndexOf($array, $item){ $indexs = array_keys($array, $item); if(count($indexs)<=0) return -1; return $indexs[0]; } /** * 数组去重 * @param type $array */ static function arrayUnique(&$array){ $array = array_values(array_unique($array)); return $array; } /** * 数组升序排列 * @param type $array */ static function arraySort(&$array){ return sort($array); } /** * 数组降序排列 * @param type $array */ static function arrayRsort(&$array){ return rsort($array); } /** * 统计数组元素数或者对象属性个数 * @param type $var * @return type */ static function count($var){ return count((array)$var); } /** * 数组随机取样 * @param type $array * @return type */ static function arrayRandom($array){ $iCount = self::count($array); $ranIndex = rand(0, $iCount - 1); return $array[$ranIndex]; } /** * 数组随机取样 * @param type $array 目标数组 * @param type $num 取样数目 */ static function arrayRandoms($array,$num){ $newarray = array(); if(self::count($array)<$num){ $num = self::count($array); } $oldarray = self::arrayClone($array); do{ $iCount = self::count($oldarray); if($iCount<=0){ break; }else{ $ranIndex = rand(0, $iCount - 1); $newarray[] = $oldarray[$ranIndex]; self::arrayRemoveAt($oldarray, $ranIndex); } }while(self::count($newarray)<$num); return $newarray; } }