Thinkphp Summary of One Dimensional Array Methods Applicable to Changing Two Dimensional Arrays into Labels

  • 2021-07-22 09:22:42
  • OfStack

This article summarizes Thinkphp's 1-D array method for changing 2-D arrays into labels. Share it for your reference. The specific implementation method is as follows:

Method 1:

$projectList=arr1tag($projectList,array('',' Please select '),'project_name');  
 
// Among them $list For passing values 2 Dimensional array, $default Is the default value, $k For 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:

$projectList=arr2tag($projectList,array('',' Please select '),'');  
 
// Get the corresponding value according to 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; 

// Converts an array from the database to an array that can use template labels, where $default Is the default value, $k For 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 contents of the read database directly to a 1-dimensional array, which is mostly used for select tags

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

I hope this article is helpful to everyone's ThinkPHP framework programming.


Related articles: