The example of this article summarizes the one-dimensional array method that thinkphp the two-dimensional array into a label. Share to everyone for your reference. The implementation methods are as follows:
Method One:
Copy Code code as follows:
$projectList =arr1tag ($projectList, Array (', ' Please choose '), ' project_name ');
Where $list is a two-dimensional array of values, $default as 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 $k 1=> $v 1) {
$tmp [$k 1+1]= $v 1[$k];
}
}
return $tmp;
}
Method Two:
Copy Code code as follows:
$projectList =arr2tag ($projectList, Array (', ' Please select ');
Gets 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;
}
Converts the array in the database into an array that can use the template label, 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 Three:
Converts the contents of a read database directly to a one-dimensional array, which is used mostly for select labels
Copy Code code as follows:
$this->where ($where)->getfield (' Id,name ');
The results are as follows
Array
' id ' => ' name ',
)
I hope this article will be helpful to everyone's thinkphp framework program design.