Thinkphp a two-dimensional array into a label application of one-dimensional array method to summarize _php instance

Source: Internet
Author: User

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.

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.