php代码
1, 'name'=> '网易', 'sex'=> 1), array('id'=> 2, 'name'=> '新浪', 'sex'=> 1), array('id'=> 3, 'name'=> '腾讯', 'sex'=> 1), array('id'=> 4, 'name'=> '阿里巴巴', 'sex'=> 1), array('id'=> 5, 'name'=> '搜狐', 'sex'=> 1)); var_dump(array_rands($array, 0)); /* 数组分段筛选 [可用于数组分页] return (array) */ function array_key_search($array, $limit = 5, $page = 0) { $page = empty($_GET['page']) || !empty($_GET['page']) && (intval($_GET['page']) < 0 || $_GET['page'] == '1') ? 0 : $_GET['page']-1; //可自己行改写 sort($array); foreach($array as $key=> $val) { if($key < $page*$limit || $key > ($page*$limit)+$limit-1) unset($array[$key]); } return $array; } /* 数组乘积 return (int) */ function array_multip($array) { $multip = 1; if(!is_array($array)) return (bool)false; if(empty($array)) return (int)0; foreach($array as $v) if(!is_numeric($v)) return false; $multip *= $v; return (int)$multip; } /* 数组取平均值 return (int) */ function array_mean($array) { if(!is_array($array)) return (bool)false; if(empty($array)) return (int)0; foreach($array as $v) if(!is_numeric($v)) return false; return array_sum($array)/count($array); } /* 随机取出数组 return (array) */ function array_rands($array, $rand = 0) { if(!is_array($array)) return (bool)false; if(empty($array) || !is_numeric($rand) || is_numeric($rand) && intval($rand) === 0) return (int)0; if(intval($rand) >= count($array)) return (array)$array; $rands = array(); while(count($rands) > intval($rand)) { $rand_indexes = rand(0, (count($array)-1)); if(array_key_exists($rand_indexes, $array))$rands[] = $array[$rand_indexes]; unset($array[$rand_indexes]); } return (array)$rands; }