search
Homephp教程php手册Thinkphp将二维数组变为标签适用的一维数组方法总结

Thinkphp将二维数组变为标签适用的一维数组方法总结

Jun 06, 2016 pm 08:18 PM
thinkphpone-dimensional arrayTwo-dimensional arraybecomeLabelBe applicable

这篇文章主要介绍了Thinkphp将二维数组变为标签适用的一维数组方法,总结了常见的转化数组方法,非常实用,需要的朋友可以参考下

本文实例总结了Thinkphp将二维数组变为标签适用的一维数组方法。分享给大家供大家参考。具体实现方法如下:

方法一:

复制代码 代码如下:

$projectList=arr1tag($projectList,array('','请选择'),'project_name'); 
 
//其中$list为传值过来的二维数组,$default为默认值,$k为指定的表字段 
function arr1tag($list,$default='',$k=''){ 
 $tmp=''; 
 if(array($list)){ 
  if(array($default)){ 
   $tmp[$default[0]]=$default[1];  
  } 
  foreach ($list as $k1=>$v1){ 
   $tmp[$k1+1]=$v1[$k]; 
     }  
 } 
 return $tmp; 
}


方法二:

复制代码 代码如下:

$projectList=arr2tag($projectList,array('','请选择'),''); 
 
//根据数组下标获取对应值 
function array_index2val($array,$index=0){ 
 $value=''; 
 if(is_array($array)){ 
  $i=0; 
  foreach($array as $val){ 
   if($i===$index){ 
    $value=$val; 
    break; 
   } 
   $i++; 
  } 
 } 
 return $value; 

//把数据库中调出的数组转换成可以使用模版标签的数组,,其中$default为默认值,$k为指定的表字段 
function arr2tag($arr,$default=NULL,$K=NULL){ 
 $tmp=''; 
 if(is_array($arr)){ 
  if(is_array($default)){ 
   $tmp[$default[0]]=$default[1]; 
   if($type==1){ 
    $tmp[$default[2]]=$default[3];  
   } 
  } 
  foreach ($arr as $key=>$val){ 
   if(is_array($K)){ 
    $tmp[$val[$K[0]]]=$val[$K[1]]; 
   }else{ 
    $tmp[array_index2val($val,0)]=array_index2val($val,1); 
   }   
  } 
 } 
 return $tmp; 
}


方法三:

将读取数据库的内容直接转换为一维数组,该方法大多用于select标签 

复制代码 代码如下:

$this->where($where)->getField('id,name'); 
得出的内容为 
array( 
 'id' => 'name', 
)

希望本文所述对大家的ThinkPHP框架程序设计有所帮助。

Statement
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)