来开始今天的php教程,主要是介绍下php中二维数组的遍历方法,代码如下:
<?php
/**
* 遍历二维数组
* edit www.
*/
//for循环遍历
$arr2=array(array("张三","20","男"),array("李四","25","男"),array("王五","19","女"),array("赵六","25","女"));
echo "<table border=2 bordercolor=red><tr><td>姓名</td><td>年龄</td& gt;<td>性别</td></tr>";
for($i=0;$i<4;$i++){
echo "<tr>";
for($j=0;$j<3;$j++){
echo "<td>";
echo $arr2[$i][$j];
echo "</td>";
}
echo "</tr>";
echo "<br>";
}
echo "</table>";
?>
//foreach遍历
$arr = array('one'=>array('name'=>'张三','age'=>'23','sex'=>'男'),
'two'=>array('name'=>'李四','age'=>'43','sex'=>'女'),
'three'=>array('name'=>'王五','age'=>'32','sex'=>'男'),
'four'=>array('name'=>'赵六','age'=>'12','sex'=>'女'));
foreach($arr as $k=>$val){
echo $val['name'].$val['age'].$val['sex']."<br>";
}
echo "<p>";
//foreach遍历 例2
$arr = array('one'=>array('name'=>'张三','age'=>'23','sex'=>'男'),
'two'=>array('name'=>'李四','age'=>'43','sex'=>'女'),
'three'=>array('name'=>'王五','age'=>'32','sex'=>'男'),
'four'=>array('name'=>'赵六','age'=>'12','sex'=>'女'));
foreach($arr as $key=>$value){
foreach($value as $key2=>$value2){
echo $value2;
}
echo "<br>";
}
?>
如何判断php中的数组是否索引数组呢?
笔者实现了一个简单的函数,供大家参考。
<?php
/**
* 检测是否索引数组
* edit www.
*/
function is_assoc($array) {
if(is_array($array)) {
$keys = array_keys()($array);
return $keys != array_keys($keys);
}
return false;
}
//调用示例
echo is_assoc($array)?'索引数组':'非索引数组';
?>
本缩略图实现的功能如下:
2,必须是规定的缩略图大小
3,原图可以裁剪但需要中心点在窗口
代码如下:
/**
* 等比例缩小的缩略图
* edit www.
*/
function thumbs($FileName,$SaveTo,$SetW,$SetH){
$IMGInfo= getimagesize($FileName);
if(!$IMGInfo) return false;
if($IMGInfo[mime]== "image/pjpeg" || $IMGInfo[mime]=="image/jpeg"){
$ThisPhoto= imagecreatefromjpeg($FileName);
}elseif($IMGInfo[mime]== "image/x-png" || $IMGInfo[mime]== "image/png"){
$ThisPhoto= imagecreatefrompng($FileName);
}elseif($IMGInfo[mime]== "image/gif"){
$ThisPhoto=imagecreatefromgif($FileName);
}
$width=$IMGInfo[0];
$height=$IMGInfo[1];
$scalc = max($width/$SetW,$height/$SetH);
$nw = intval($width/$scalc);
$nh = intval($height/$scalc);
echo "缩略大小:w$nw,h$nh <br />";
if($SetW-$nw == 1){$nw = $SetW;}
if($SetH-$nh == 1){$nh = $SetH;}
echo "+修正1像素: w$nw,h$nh<br />";
//补宽
if($SetW-$nw > 0){
$nh = $nh +(($nh/$nw) * ($SetW-$nw));
echo "*需补宽".($SetW-$nw).",陪补高".(($nh/$nw) * ($SetW-$nw))." <br />";
$nw = $SetW;
}
//补高
if($SetH-$nh > 0){
$nw = $nw + (($nw/$nh) * ($SetH-$nh));
echo "*需补高".($SetH-$nh).",陪补宽". (($nw/$nh) * ($SetH-$nh)) ."<br />";
$nh = $SetH;
}
$nw = intval($nw);
$nh = intval($nh);
echo "+修正大小:w$nw,h$nh<br />";
$px = ($SetW - $nw)/2;
$py = ($SetH - $nh)/2;
echo "窗口大小:w$SetW,h$SetH <br />";
echo "+偏移修正:x$px,y$py <br />";
$NewPhoto=imagecreatetruecolor($SetW,$SetH);
imagecopyresized($NewPhoto,$ThisPhoto,$px,$py,0,0,$nw,$nh,$width,$height);
ImageJpeg ($NewPhoto,$SaveTo);
return true;
}
thumbs('a.jpg','newa.jpg',150,70);
?>
2,css代码部
img{ border:1px solid #ccc;margin:2px;}
</style>
3,html页面中的图片地址
<img src="/newa.jpg" />