Home >Backend Development >PHP Tutorial >Summary of Thinkphp's method of converting a two-dimensional array into a one-dimensional array suitable for labels, thinkphp dimensions_PHP tutorial

Summary of Thinkphp's method of converting a two-dimensional array into a one-dimensional array suitable for labels, thinkphp dimensions_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:15:44898browse

Thinkphp summary of the method of converting a two-dimensional array into a one-dimensional array suitable for labels, thinkphp dimensions

The example in this article summarizes Thinkphp's method of converting a two-dimensional array into a one-dimensional array suitable for tags. Share it with everyone for your reference. The specific implementation method is as follows:

Method 1:

Copy code The code is as follows:
$projectList=arr1tag($projectList,array('','Please select'),'project_name ');

//Where $list is the passed two-dimensional array, $default is the default value, and $k is the specified table field
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;
}

Method 2:

Copy code The code is as follows:
$projectList=arr2tag($projectList,array('','Please select'),'' );

//Get the corresponding value based on the array subscript
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;
}
//Convert the array called out from the database into an array that can use template tags, where $default is the default value and $k is the specified table field
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;
}

Method 3:

Convert the content read from the database directly into a one-dimensional array. This method is mostly used for select tags

Copy code The code is as follows:
$this->where($where)->getField('id,name');
The resulting content is
array(
'id' => 'name',
)

I hope this article will be helpful to everyone’s ThinkPHP framework programming.

How to use ThinkPHP program to convert such a two-dimensional array into a one-dimensional array

I don’t quite understand what effect you want to achieve with a one-dimensional array. It’s best to simulate it so that everyone can better help you answer it.
The effect of your supplement is the same as before!

How to convert a one-dimensional array into a two-dimensional array in PHP

$re=array();for($i=0;$i

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/903476.htmlTechArticleThinkphp summarizes the method of turning a two-dimensional array into a one-dimensional array suitable for tags, thinkphp dimensions This article summarizes Thinkphp Method to convert a two-dimensional array into a one-dimensional array suitable for labels. Share with everyone...
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