首页 > Web开发 > 详细

php数组练习十六道题

时间:2014-08-26 19:20:16      阅读:481      评论:0      收藏:0      [点我收藏+]
<?php
/**
 * 1、写函数创建长度为10的数组。数组中的元素为递增的奇数。首项为1。
 */
$ary = range(1, 10);
//Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 ) 
print_r($ary);

/**
 * 2、创建长度为10的数组,数组中的元素为递增的等比数,比值为3。首项为1
 */
function myfun($n){
	return pow(3, $n-1);
}
$ary = range(1, 10);
$result = array_map(‘myfun‘,$ary);
echo ‘<br>‘;
//Array ( [0] => 1 [1] => 3 [2] => 9 [3] => 27 [4] => 81 [5] => 243 [6] => 729 [7] => 2187 [8] => 6561 [9] => 19683 ) 
print_r($result);

/**
 * 3、求数组中最大数所在的下标;
 * $arr = array(1,5,67,8,4,3,45,6,887,2);
 */
$arr = array(1,5,67,8,4,3,45,6,887,2);
$max = $arr[0];//假设第一个数是最大的
$index = 0;//记录最大数的key值,即下标
foreach ($arr AS $key=>$val) {
	if ($max < $val) {
		$max = $val;
		$index = $key;
	}
}
//最大数是:887,其下标是:8
echo ‘<br>最大数是:‘.$max . ‘,其下标是:‘ . $index;
/* $mval = max($arr);
echo ‘<br>‘;
print_r(array_search($mval, $arr)); */

/**
 * 4、创建一个长度为10的数组,数组中的元素满足斐波拉齐数列的规律;
 */
$arr = array(1,1);
for ($i=2; $i<10; $i++) {
	$arr[] = $arr[$i-1]+$arr[$i-2];
}
echo ‘<br>‘;
//Array ( [0] => 1 [1] => 1 [2] => 2 [3] => 3 [4] => 5 [5] => 8 [6] => 13 [7] => 21 [8] => 34 [9] => 55 ) 
print_r($arr);

/**
 * 5、计算数组中最大数和最小数的差;
 */
$arr = array(1,5,67,8,4,3,45,6,887,2);
/*系统函数简单实现
 * 
$minv = min($arr);
$maxv = max($arr);
echo ‘<br>‘;
echo ‘最大数是:‘ . $maxv .‘,最小数是:‘.$minv.‘,差是:‘.($maxv-$minv);
 */

/*不使用系统函数实现*/
$max = $arr[0];//假设第一个数是最大的
$min = $arr[0];//假设第一个数也是最小的
foreach ($arr AS $key=>$val) {
	if ($max < $val) {
		$max = $val;
	} elseif ($min > $val) {
		$min = $val;
	}
}
echo ‘<br>‘;
//最大数是:887,最小数是:1,差是:886
echo ‘最大数是:‘ . $max .‘,最小数是:‘.$min.‘,差是:‘.($max-$min);

/**
 * 6、写一个方法,将一个长度超过10的数组最后5项直接截取,不改变顺序直接变为前5项,
 * 如{1,2,3,4,5,6,7,8,9,10,11,12}在经过方法后变为:{8,9,10,11,12,1,2,3,4,5,6,7}
 */
function arrayHandel(&$ary, $count = 5) {
	if (!(is_array($ary) && count($ary)>10 && count($ary)>$count)) {
		return false;
	}
	$len = count($ary);
	while ($count>0) {
		$i = $len-1;
		$count--;
		array_unshift($ary, $ary[$i]);
		array_pop($ary);
	}
}
$array = array(1,2,3,4,5,6,7,8,9,10,11,12,13);
echo ‘<br>‘;
arrayHandel($array);
//Array ( [0] => 9 [1] => 10 [2] => 11 [3] => 12 [4] => 13 [5] => 1 [6] => 2 [7] => 3 [8] => 4 [9] => 5 [10] => 6 [11] => 7 [12] => 8 ) 
print_r($array);

/**
 * 
 * 7、将两个数组连接成一个新数组;
 * array_combine($keys, $values) 一个作键,一个作值,但是两个数组个数必须一致,不然会返回false
 * array_merge($array1)  覆盖相同的键名
 * array_merge_recursive($array1) 不覆盖相同键名,组成以键名组成的数组集合
 * 
 */
$arr1 = array(‘a‘=>123,‘b‘=>1234);
$arr2 = array(‘os‘=>‘linux‘,‘db‘=>‘mysql‘,‘language‘=>‘php‘,‘server‘=>‘nginx‘,‘b‘=>1256);
echo ‘<br>‘;
//Array ( [a] => 123 [b] => 1234 [os] => linux [db] => mysql [language] => php [server] => nginx ) 
print_r($arr1+$arr2);//$arr1会覆盖$arr2中相同键值,运算是从右至左,所以左边的覆盖右边
echo ‘<br>‘;
//boolean false
var_dump(array_combine($arr1,$arr2));//boolean false
echo ‘<br>‘;
//Array ( [a] => 123 [b] => 1256 [os] => linux [db] => mysql [language] => php [server] => nginx ) 
print_r(array_merge($arr1,$arr2));//$arr1和$arr2中的相同的元素被后一个$arr2的覆盖
echo ‘<br>‘;
//Array ( [a] => 123 [b] => Array ( [0] => 1234 [1] => 1256 ) [os] => linux [db] => mysql [language] => php [server] => nginx )
print_r(array_merge_recursive($arr1,$arr2));

/**
 * 数组逆序(不能直接使用rsort函数,不能生成新数组);
 */
$ary = array(‘apple‘,‘juice‘,‘orange‘,‘85‘,‘0‘,‘app‘,‘1‘,‘2‘,‘5‘,‘z‘);
/* rsort($ary);
echo ‘<br>‘;
print_r($ary); */
for ($i=0;$i<count($ary);$i++){
	for ($j=$i+1;$j<count($ary);$j++){
		if($ary[$i]<$ary[$j]){
			$temp = $ary[$i];
			$ary[$i] = $ary[$j];
			$ary[$j] = $temp;
		}
	}
}
echo ‘<br>‘;
//Array ( [0] => z [1] => orange [2] => juice [3] => apple [4] => app [5] => 85 [6] => 5 [7] => 2 [8] => 1 [9] => 0 ) 
print_r($ary);

/** * 9、在长度为10的升序有序数组中插入一个数字,组成新数组,保证数组顺序(不能直接使用sort) */ $arr = range(1, 40, 2); function insertAry($ary, $val) { $endAry = array(); $len = count($ary); $index = 0; if ($ary[$len-1] < $val) {//若插入数比数组中任意数大 $ary[] = $val; $endAry = $ary; return $endAry; } while ($index<$len) { if ($ary[$index]>=$val){ $endAry[$index++] = $val; for($i = $index;$i<$len+1;$i++){ $endAry[$i] = $ary[$i-1]; } break; } else { $endAry[$index] = $ary[$index]; } $index++; } return $endAry; } echo ‘<br>‘; //Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9 [5] => 11 [6] => 13 [7] => 15 [8] => 17 [9] => 19 [10] => 21 [11] => 23 [12] => 25 //[13] => 27 [14] => 29 [15] => 31 [16] => 33 [17] => 35 [18] => 37 [19] => 39 [20] => 88 ) print_r(insertAry($arr,88));
/** * 10、将一个无序数组排列成升序数组;(冒泡排序) */ $arr = array(888,-9,1,11,5,67,8,4,3,45,6,887,2); $len = count($arr); for ($i=0; $i<$len; $i++) { for ($j=$len-1; $j>$i; $j--) { if ($arr[$j]<$arr[$j-1]) { $temp = $arr[$j]; $arr[$j] = $arr[$j-1]; $arr[$j-1] = $temp; } } } echo ‘<br>‘; //Array ( [0] => -9 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 8 [8] => 11 [9] => 45 [10] => 67 [11] => 887 [12] => 888 ) print_r($arr); /*********选择排序************/ $arr = array(888,-9,1,11,5,67,8,4,3,45,6,887,2); for ($i=0; $i<$len; $i++) { for ($j=$i+1; $j<$len; $j++) { if ($arr[$j]<$arr[$i]) { $temp = $arr[$j]; $arr[$j] = $arr[$i]; $arr[$i] = $temp; } } } echo ‘<br>‘; //Array ( [0] => -9 [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 [6] => 6 [7] => 8 [8] => 11 [9] => 45 [10] => 67 [11] => 887 [12] => 888 ) print_r($arr); /** * 11、对有序数组进行洗牌操作;(不能直接使用shuffle函数) */ $arr = range(1, 40, 2); echo ‘<br>‘; echo ‘<br>$arr打乱前:‘; //$arr打乱前:Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9 [5] => 11 [6] => 13 [7] => 15 [8] => 17 [9] => 19 [10] => 21 [11] => 23
//[12] => 25 [13] => 27 [14] => 29 [15] => 31 [16] => 33 [17] => 35 [18] => 37 [19] => 39 ) print_r($arr); function shuffleDemo($arr) { $resultAry = array(); $len = count($arr); while ($len>0) { $index = rand(0, $len-1); $resultAry[] = $arr[$index]; array_splice($arr, $index,1); $len--; } return $resultAry; } echo ‘<br>‘; echo ‘<br>$arr打乱后:‘; //$arr打乱后:Array ( [0] => 3 [1] => 33 [2] => 11 [3] => 15 [4] => 5 [5] => 39 [6] => 1 [7] => 25 [8] => 35 [9] => 29 [10] => 13 [11] => 27
//[12] => 23 [13] => 7 [14] => 37 [15] => 19 [16] => 9 [17] => 17 [18] => 31 [19] => 21 ) print_r(shuffleDemo($arr));
/** * 12、删除数组中指定位置的元素;(参考unset函数) */ function deleteAryElement(&$arr, $index){ if (!(is_array($arr) && count($arr)>$index)) { return false; } unset($arr[$index]); } $arr = array(888,-9,1,11,5,67,8,4,3,45,6,887,2); deleteAryElement($arr,2); echo ‘<br>‘; //Array ( [0] => 888 [1] => -9 [3] => 11 [4] => 5 [5] => 67 [6] => 8 [7] => 4 [8] => 3 [9] => 45 [10] => 6 [11] => 887 [12] => 2 ) print_r($arr);
/** * 13、将数组中素数删除组成新数组 */ function filtFun($val) {//自定义过滤素数函数 $flag = true;//判断是否是素数 if (is_int($val)) { if ($val<=0) { $flag = false; } for ($i=$val-1;$i>1;$i--) { if ($val%$i==0) { $flag = false; break; } } } else { $flag = false; } return !$flag; } $arr = array(888,-9,1,11,5,67,8,4,3,45,6,887,2,‘a‘); $rary = array_filter($arr, ‘filtFun‘); echo ‘<br>‘; //Array ( [0] => 888 [1] => -9 [6] => 8 [7] => 4 [9] => 45 [10] => 6 [13] => a ) print_r($rary);
/** * 14、写函数检查数组中是否存在某个元素(不能直接使用in_array) */ function exsitEle($arr, $value) { if (!(is_array($arr) && count($arr)>0)) { return false; } foreach ($arr AS $key=>$val) { if ($val==$value) { return true; } } return false; } $arr = array(888,-9,1,11,5,67,8,4,3,45,6,887,2,‘a‘,array(‘a‘=>14,‘b‘=>48)); //boolean true var_dump(exsitEle($arr, array(‘a‘=>14,‘b‘=>48))); /** * 15、写函数,向数组最后追加一个元素(不能直接使用array_push) */ $arr = array(888,-9,1,11,5,67,8,4,3,45,6,887,2,‘a‘,array(‘a‘=>14,‘b‘=>48)); $arr[] = ‘abcd‘; echo ‘<br>‘; //Array ( [0] => 888 [1] => -9 [2] => 1 [3] => 11 [4] => 5 [5] => 67 [6] => 8 [7] => 4 [8] => 3 [9] => 45 [10] => 6 //[11] => 887 [12] => 2 [13] => a [14] => Array ( [a] => 14 [b] => 48 ) [15] => abcd ) print_r($arr); /** * 16、写函数,求数组中所有元素之和(不能直接使用array_sum) */ $arr = array(888,-9,1,11,5,67,8,4,3,45,6,-102); function countAry($arr) { if (!(is_array($arr) && count($arr)>0)) { return false; } $sum = 0; foreach ($arr AS $key=>$val) { $sum += $val; } return $sum; } echo ‘<br>‘; //927 print_r(countAry($arr));

  php数组功能非常强大,自己对php数组的练习学习。以后也可以在此加深印象,巩固学习!

php数组练习十六道题

原文:http://www.cnblogs.com/age0420/p/3937742.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!