Thinkphp converts a two-dimensional array into a one-dimensional array method applicable to tags. thinkphp dimension

Source: Internet
Author: User

Thinkphp converts 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:

Copy codeThe 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:

Copy codeThe 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.
Copy codeThe 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 <lenth; $ I ++) $ re [$ I] = array_slice ($ arr, 5 * $ I, 5); print_r ($ re );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.