Thinkphp converts a two-dimensional array into a one-dimensional array method applicable to tags. thinkphp dimension. Thinkphp converts a two-dimensional array into a one-dimensional array method applicable to tags. thinkphp dimension this article provides an example of Thinkphp's one-dimensional array method applicable to tags. Share with Thinkphp to convert a two-dimensional array into a one-dimensional array method applicable to tags. thinkphp dimension
This example summarizes Thinkphp's one-dimensional array method for converting a two-dimensional array into a tag. Share it with you for your reference. The specific implementation method is as follows:
Method 1:
The code is as follows:
$ ProjectList = arr1tag ($ projectList, array ('', 'Select '), 'Project _ name ');
// $ List is a two-dimensional array with passed values, $ 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:
The code is as follows:
$ ProjectList = arr2tag ($ projectList, array ('', 'Select '),'');
// Obtain 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 in the database to an array that can use template labels. $ 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:
Converts the read data from the database to a one-dimensional array. this method is mostly used for select tags.
The code is as follows:
$ This-> where ($ where)-> getField ('Id, name ');
The result is as follows:
Array (
'Id' => 'name ',
)
I hope this article will help you with ThinkPHP framework programming.
How to use the ThinkPHP program to change such a two-dimensional array to a one-dimensional array
I don't quite understand what you want to achieve in your one-dimensional array. it is best to simulate it so that you can better help you solve it.
Your supplement has the same effect as before!
How does PHP convert a one-dimensional array into a two-dimensional array?
$ Re = array (); for ($ I = 0; $ I
In this example, Thinkphp converts a two-dimensional array into a one-dimensional array method applicable to tags. Share with me...