php 把二维数组保存到一维数组教程

 更新时间:2016年11月25日 16:29  点击:2018

$array = array(
    array(1,2),
    array(3,4),
    array('www.111cn.net','111cn.net')
    );
    
//看到上面二维数结构了吧,下面我们用foreach来实例

function array_2to1($array)
{
    static $result_array=array();
    foreach($array as $value)
    {
        if(is_array($value))
        {
            arrau_2to1($value);
        }
        else 
            $result_array[]=$value;
    }
    return $result_array;
}

//上面这段代码还可以简写

function _rebuild_array($arr){  //rebuild a array
  static $tmp=array();

  for($i=0; $i<count($arr); $i++){
    if(is_array($arr[$i])) _rebuild_array($arr[$i]);
    else $tmp[]=$arr[$i];
  }

  return $tmp;
}


$arr = array_2to1( $array );
foreach( $arr as $v )
{
 echo $v;
}

//现在再来看一个反一维数据保存到二维数据的实例

$arr_new=array();
$insert_key  =array('uid','hostname','shopname','province','city','county','address','www.111cn.net','qq','Mobile','msn');

$insert_value=array('2','hostname','shopname','province','city','www.111cn.net','address','shopimg','qq','Mobile','msn');
//一在我们把2个数组一一对应写到一个新的二维数组里去

foreach($insert_key as $key => $val){
$arr_new[$val]=$insert_value[$key];

}
print_r($arr_new);

function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$RESIZEWIDTH=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$RESIZEHEIGHT=true;
}
if($RESIZEWIDTH && $RESIZEHEIGHT){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($RESIZEWIDTH){
$ratio = $widthratio;
}elseif($RESIZEHEIGHT){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$smalladdrname.$name.".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$smalladdrname.$name.".jpg");
}
}

 

if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($bigaddrname.$exname);
}elseif($_FILES['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($bigaddrname.$exname);
}elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($bigaddrname.$exname);
}
if($im){
if(file_exists($smalladdrname.".jpg")){

unlink($smalladdrname.".jpg");
}
ResizeImage($im,$RESIZEWIDTH,$RESIZEHEIGHT,$smalladdrname);
ImageDestroy ($im);
}

php教程按指定大小等比缩放生成上传图片缩略图
/**
 * *
 *等比缩放
 * @param unknown_type $srcImage   源图片路径
 * @param unknown_type $toFile     目标图片路径
 * @param unknown_type $maxWidth   最大宽
 * @param unknown_type $maxHeight  最大高
 * @param unknown_type $imgQuality 图片质量
 * @return unknown
 */

function resize($srcImage,$toFile,$maxWidth = 100,$maxHeight = 100,$imgQuality=100)
{
 
    list($width, $height, $type, $attr) = getimagesize($srcImage);
    if($width < $maxWidth  || $height < $maxHeight) return ;
    switch ($type) {
    case 1: $img = imagecreatefromgif($srcImage); break;
    case 2: $img = imagecreatefromjpeg($srcImage); break;
    case 3: $img = imagecreatefrompng($srcImage); break;
    }
    $scale = min($maxWidth/$width, $maxHeight/$height); //求出绽放比例
   
    if($scale < 1) {
    $newWidth = floor($scale*$width);
    $newHeight = floor($scale*$height);
    $newImg = imagecreatetruecolor($newWidth, $newHeight);
    imagecopyresampled($newImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
    $newName = "";
    $toFile = preg_replace("/(.gif|.jpg|.jpeg|.png)/i","",$toFile);

    switch($type) {
        case 1: if(imagegif($newImg, "$toFile$newName.gif", $imgQuality))
        return "$newName.gif"; break;
        case 2: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
        return "$newName.jpg"; break;
        case 3: if(imagepng($newImg, "$toFile$newName.png", $imgQuality))
        return "$newName.png"; break;
        default: if(imagejpeg($newImg, "$toFile$newName.jpg", $imgQuality))
        return "$newName.jpg"; break;
    }
    imagedestroy($newImg);
    }
    imagedestroy($img);
    return false;
}

//英文验证码相对简单,不要作hex处理,直接用色彩值就OK了。如果
session_start();
function rand_create()
{
    //通知浏览器将要输出PNG图片
    Header("Content-type: image/PNG");
    //准备好随机数发生器种子 
    srand((double)microtime()*1000000);
    //准备图片的相关参数  
    $im = imagecreate(62,22);
    $black = ImageColorAllocate($im, 0,0,0);  //RGB黑色标识符
    $white = ImageColorAllocate($im, 255,255,255); //RGB白色标识符
    $gray = ImageColorAllocate($im, 200,200,200); //RGB灰色标识符
    //开始作图    
    imagefill($im,0,0,$gray);
    while(($randval=rand()%100000)<10000);{
        $_SESSION["Auth_code"] = $randval;
        //将四位整数验证码绘入图片 
        imagestring($im, 5, 10, 3, $randval, $black);
    }
    //加入干扰象素   
    for($i=0;$i<200;$i++){
        $randcolor = ImageColorallocate($im,rand(0,255),rand(0,255),rand(0,255));
        imagesetpixel($im, rand()%70 , rand()%30 , $randcolor);
    }
    //输出验证图片
    ImagePNG($im);
    //销毁图像标识符
    ImageDestroy($im);
}
rand_create();

//调用方法<img src=www.111cn.net.php />

//中文验证码程序如下

Header("Content-type: image/PNG");
$str = "这里设置一中文如果中国WEB第一站www.111cn.net";
$imagesW = 140;
$imagesH = 40;
//
$Auimg = imagecreate($imagesW,$imagesH);
$bgc = ImageColorAllocate($Auimg,255,255,255);
$font = "heiti.ttf";//这里设置字体,你可以随便下载一款字体哦。
$white=imagecolorallocate($Auimg,234,185,95);
imagearc($Auimg, 150, 8, 20, 20, 75, 170, $white);
imagearc($Auimg, 180, 7,50, 30, 75, 175, $white);
imageline($Auimg,20,20,180,30,$white);
imageline($Auimg,20,18,170,50,$white);
imageline($Auimg,25,50,80,50,$white);
$noise_num = 800;
$line_num = 20;
imagecolorallocate($Auimg,0xff,0xff,0xff);
$rectangle_color=imagecolorallocate($Auimg,0xAA,0xAA,0xAA);
$noise_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
$font_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
$line_color=imagecolorallocate($Auimg,0x00,0x00,0x00);
for($i=0;$i<$noise_num;$i++){
 imagesetpixel($Auimg,mt_rand(0,$imagesW),mt_rand(0,$imagesH),$noise_color);
}
for($i=0;$i<$line_num;$i++){
 imageline($Auimg,mt_rand(0,$imagesW),mt_rand(0,$imagesH),mt_rand(0,$imagesW),mt_rand(0,$imagesH),$line_color);
}
$mtRnd=rand(0,strlen($str)-4);
if($mtRnd%2)$mtRnd+=1;
$str = substr($str,$mtRnd,8);
$str = iconv("GB2312","UTF-8",$str);
ImageTTFText($Auimg, 20, 0, 16, 30, $font_color, $font, $str);
ImagePNG($Auimg);
ImageDestroy($Auimg);
/*
 共同点就是验证码都借助于其它容器来保存如session,cookie等,否则就没有验证的意义了
 本文章由www.111cn.net整,转载请注明来源谢谢合作。

 

//$pattern = "/file-([0-2]d{3})-([1-9]d?)/"; 这个正则表达式,对文件夹的月日进行捕获,再进一步判断处理 

02 for($i =2005;$i<=2009;$i++) 

03 { 

04 for($j = 1;$j<30;$j++) 

05 { 

06 $dest_dir = "text/file-".$i."-$j"; 

07 if(!is_dir($dest_dir)) mkdir($dest_dir,0777); 

08 } 

09 } 

10 //创建一些不满足条件的文件夹 

11   

12 for($t = 0;$t<10;$t++) 

13 { 

14 $dest_dir = "text/file-".$t; 

15 if(!is_dir($dest_dir)) mkdir($dest_dir,0777); 

16 } 

17   

18 //遍历文件夹处理 . 

19 function listFile($dir) 

20 {  

21 $a = array(); 

22 $handle = opendir($dir); 

23 while($file = readdir($handle)) 

24 { 

25 handle_dir($dir,$file,&$a); 

26 } 

27   

28 echo "the rege_array is "; 

29 print("<pre>"); 

30 print_r($a); 

31 print("</pre>"); 

32 } 

33   

34 function handle_dir($dir,$file,$a) 

35 { 

36   

37 if($file == "." || $file == "..") return ; 

38 $minYear = 2008; 

39 $maxDay = 13; 

40   

41 $pattern = "/file-([0-2]d{3})-([1-9]d?)/"; 

42 $destPath = $dir."/".$file; 

43   

44 if(!is_dir($destPath)) 

45 { 

46 //echo "$destPath is not a dir ;"; 

47 return; 

48 } 

49   

50 //删除不满足格式的文件 

51 if(!preg_match($pattern,$file)) 

52 { 

53 //echo "<font color=blue>$file</font><font color=red>is not the right rege. the program will unlink -- $destPath --</font><br/>"; 

54 // unlink($destPath); 

55 return; 

56 } 

57   

58 preg_match_all($pattern,$file,$matchs); 

59   

60 //echo "<font color=blue>$file</font> is the right rege"; 

61 //print_r($matchs); 

62 //echo "the year is ".$matchs[1][0]." and the day is ".$matchs[2][0]."</br>"; 

63 if(intval($matchs[1][0]) >$minYear && intval($matchs[2][0]) < $maxDay) 

64 { 

65 $a[]= $destPath; 

66 }else { //不满足条件的 

67 //unlink($destPath); 

68   

69 } 

70 } 

71   

72 listFile("text");

[!--infotagslink--]

相关文章

  • C#从数据库读取图片并保存的两种方法

    这篇文章主要介绍了C#从数据库读取图片并保存的方法,帮助大家更好的理解和使用c#,感兴趣的朋友可以了解下...2021-01-16
  • C#二维数组基本用法实例

    这篇文章主要介绍了C#二维数组基本用法,以实例形式分析了C#中二维数组的定义、初始化、遍历及打印等用法,具有一定参考借鉴价值,需要的朋友可以参考下...2020-06-25
  • PHP 二维数组根据某个字段排序的具体实现

    本文记录的要实现的功能类似于 MySQL 中的 ORDER BY,上个项目中有遇到这样的一个需求。 要求:从两个不同的表中获取各自的4条数据,然后整合(array_merge)成一个数组,再根据数据的创建时间降序排序取前4条。 遇到这个...2014-06-07
  • 将二维数组转为一维数组的2种方法

    如何将下面的二维数组转为一维数组。复制代码 代码如下:$msg = array(  array(    'id'=>'45',    'name'=>'jack'  ),  array(    'id'=>'34',    'name'=>'mary'  ),  array(    'id...2014-05-31
  • python读取和保存mat文件的方法

    本文主要介绍了python读取和保存mat文件的方法,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-08-25
  • PHP 如何获取二维数组中某个key的集合

    本文为代码分享,也是在工作中看到一些“大牛”的代码,做做分享。 具体是这样的,如下一个二维数组,是从库中读取出来的。 代码清单: 复制代码 代码如下: $user = array( 0 => array( 'id' => 1, 'name' => '张三', 'ema...2014-06-07
  • python 实现将Numpy数组保存为图像

    今天小编就为大家分享一篇python 实现将Numpy数组保存为图像,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-04-27
  • 安卓开发之保存ImageView中的图片到本地相册

    下面我们来看一篇关于安卓开发之保存ImageView中的图片到本地相册教程吧,希望这篇教程能够给大家带来帮助. 代码如下. private void saveImage(ImageView imageVi...2016-11-01
  • C#抓取网络图片保存到本地的实现方法

    下面小编就为大家分享一篇C#抓取网络图片保存到本地的实现方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-25
  • C读txt到二维数组的实现方法

    下面小编就为大家带来一篇C读txt到二维数组的实现方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-04-25
  • php把txt文本转换成数组并保存数据库

    今天有一个工作要做就是把一个达5万条的记录的txt 文件的内容要保存到数据库,开始想到用file_get_contents后来就看到可以用file更简单,下面是我写的程序. <?php inc...2016-11-25
  • pytorch模型的保存和加载、checkpoint操作

    这篇文章主要介绍了pytorch模型的保存和加载、checkpoint操作,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-06-06
  • OpenCV获取视频的每一帧并保存为.jpg图片

    这篇文章主要为大家详细介绍了OpenCV获取视频的每一帧,并保存为.jpg图片,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-04-25
  • Python3中PyQt5简单实现文件打开及保存

    本文将结合实例代码,介绍Python3中PyQt5简单实现文件打开及保存,具有一定的参考价值,需要的朋友们下面随着小编来一起学习学习吧...2021-06-10
  • php二维数组排序后获取最大值

    PHP一维数组的排序可以用sort(),asort(),arsort()等函数,但是PHP二维数组的排序需要自定义。 自定义: 代码如下 复制代码 function array_sort...2016-11-25
  • PHP二维数组去重算法

    本文给大家分享的代码是php实现的二维数组根据键值合并并去重复的算法,非常实用,有需要的小伙伴可以参考下...2017-01-08
  • php读取和保存base64编码的图片内容

    这篇文章主要为大家详细介绍了php读取和保存base64编码的图片内容,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2017-04-26
  • C语言中二维数组作为函数参数来传递的三种方法

    这篇文章主要给大家介绍了关于C语言中二维数组作为函数参数来传递的三种方法,文中通过示例代码介绍的非常详细,对大家学习或者使用C语言有一定的参考学习价值,需要的朋友们下面来一起学习学习吧...2020-04-25
  • php 连接odbc数据源并保存 查询数据

    代码如下 复制代码 $connstr = "driver=microsoft access driver (*.mdb);dbq=".realpath("db.mdb"); $connid = odbc_connect($connstr,"",...2016-11-25
  • Go语言二维数组的传参方式

    这篇文章主要介绍了Go语言二维数组的传参方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-04-28