1 // 生成 html select option 标签
2 function build_options(
$options,
$opt='',
$k=''
) {
3 $frags =
array();
4 if (
is_string(
$options))
5 {
6 if (
FALSE !==
strpos(
$options, ','
)) {
7 $options =
explode(',',
$options);
8 }
else if (
FALSE !==
strpos(
$options, ' '
)) {
9 $options =
explode(' ',
$options);
10 }
else{
11 return ''
;
12 }
13 if (
$opt && !
in_array(
$opt,
$options))
14 {
15 array_unshift(
$options,
$opt);
16 }
17
18 $options =
array_combine(
$options,
$options);
19 }
20 if (
is_array(
$options))
21 {
22 if(
$opt && !
in_array(
$opt,
$options))
23 {
24 array_unshift(
$options,
$opt);
25 }
26 foreach (
$options as $key=>
$val)
27 {
28 if (
$opt!==''&&
$k!==''&&
$key ==
$opt)
29 {
30 //后来添加的那个元素中还有键 说明传入了k
31 $key=
$k;
32 }
33 $frags[] =
sprintf('<option value="%s" %s>%s</option>',
$key,
$opt != '' &&
$key ==
$opt ? 'selected' : '',
$val);
34 }
35 }
36
37 return implode("\n",
$frags);
38 }
传入两个参数 第一个是数组 第二个是要选中的列key值
如果只是传入字符串在这样的
同时如果要传入键值为数组中没有的可以这样做
默认选中你需要的
转载于:https://www.cnblogs.com/lizhaoyao/p/4304737.html