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.
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!
$re=array();for($i=0;$i
http://www.bkjia.com/PHPjc/903476.htmlwww.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...