1:二维数组查询
<?php
/*echo "alter table table_name add name varchar(20)";echo "alter table table_name add unique name";//为name列添加唯一索引
echo "alter table table_name add index(a,b,c)"//*/
function findNumber($arr,$key){
$row=0;
$rows=count($arr);
$column=count($arr)-1;
while($row<$rows && $column>=0){
if($arr[$row][$column]==$key){
return 1;
}elseif($arr[$row][$column]>$key){
$column--;
}else{
$row++;
}
}
return 0;
}
$arr=array(array(1,2,3),array(4,5,6),array(7,8,9));
echo findNumber($arr,10);
2:计算和组合
<?php
$arr=array(1,7,17,2,6,3,14,16,4);
for($i=0;$i<count($arr);$i++){
for($j=$i+1;$j<count($arr);$j++){
if($arr[$i]+$arr[$j]==20){
echo $arr[$i].','.$arr[$j];
echo '<br/>';
}
}
}
sort($arr);
print_r($arr);
echo '<br/>';
$begin=0;
$end=count($arr)-1;
while($begin<=$end){
if($arr[$begin]+$arr[$end]==20){
echo $arr[$begin].','.$arr[$end];
$begin++;
$end--;
echo '<br/>';
}elseif($arr[$begin]+$arr[$end]<20){
$begin++;
}else{
$end--;
}
}
3://求和
function sum($n){
$sum=pow($n, 2)+$n;
return $sum>>1;
}
echo sum(1000);
4:
<?php
//数组中的最大最小值
$arr=array(74, 48, 30, 17, 62);
$min=$arr[0];
$max=$arr[0];
for($i=1;$i<count($arr);$i++){
if($min>$arr[$i]){
$min=$arr[$i];
}
if($max<$arr[$i]){
$max=$arr[$i];
}
}
echo $min.'->'.$max;