Home > php教程 > PHP源码 > body text

PHP数组操作函数

PHP中文网
Release: 2016-05-25 17:09:28
Original
983 people have browsed it

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[&#39;page&#39;]) || !empty($_GET[&#39;page&#39;]) && (intval($_GET[&#39;page&#39;]) < 0 || $_GET[&#39;page&#39;] == &#39;1&#39;) ? 0 : $_GET[&#39;page&#39;]-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;
}
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template