4000-520-616
欢迎来到免疫在线!(蚂蚁淘生物旗下平台)  请登录 |  免费注册 |  询价篮
主营:原厂直采,平行进口,授权代理(蚂蚁淘为您服务)
咨询热线电话
4000-520-616
当前位置: 首页 > 新闻动态 >
热卖商品
新闻详情
PHP: array - Manual
来自 : www.php.net/manual/zh/functi.. 发布时间:2021-03-25
The following function (similar to one above) will render an array as a series of HTML select options (i.e. \" option ... /option \"). The problem with the one before is that there was no way to handle optgroup , so this function solves that issue.

function arrayToSelect($option, $selected = \'\', $optgroup = NULL)
{
$returnStatement = \'\';

if ($selected == \'\') {
$returnStatement .= \' option value=\"\" selected=\"selected\" Select one... /option
}

if (isset($optgroup)) {
foreach ($optgroup as $optgroupKey = $optgroupValue) {
$returnStatement .= \' optgroup label=\"\' . $optgroupValue . \'\"

foreach ($option[$optgroupKey] as $optionKey = $optionValue) {
if ($optionKey == $selected) {
$returnStatement .= \' option selected=\"selected\" value=\"\' . $optionKey . \'\" \' . $optionValue . \' /option
} else {
$returnStatement .= \' option value=\"\' . $optionKey . \'\" \' . $optionValue . \' /option
}
}

$returnStatement .= \' /optgroup
}
} else {
foreach ($option as $key = $value) {
if ($key == $selected) {
$returnStatement .= \' option selected=\"selected\" value=\"\' . $key . \'\" \' . $value . \' /option
} else {
$returnStatement .= \' option value=\"\' . $key . \'\" \' . $value . \' /option
}
}
}

return $returnStatement;
}

So, for example, I needed to render a list of states/provinces for various countries in a select field, and I wanted to use each country name as an optgroup label. So, with this function, if only a single array is passed to the function (i.e. \"arrayToSelect($stateList)\") then it will simply spit out a bunch of \" option \" elements. On the other hand, if two arrays are passed to it, the second array becomes a \"key\" for translating the first array.

Here\'s a further example:

$countryList = array(
\'CA\' = \'Canada\',
\'US\' = \'United States\');

$stateList[\'CA\'] = array(
\'AB\' = \'Alberta\',
\'BC\' = \'British Columbia\',
\'AB\' = \'Alberta\',
\'BC\' = \'British Columbia\',
\'MB\' = \'Manitoba\',
\'NB\' = \'New Brunswick\',
\'NL\' = \'Newfoundland/Labrador\',
\'NS\' = \'Nova Scotia\',
\'NT\' = \'Northwest Territories\',
\'NU\' = \'Nunavut\',
\'ON\' = \'Ontario\',
\'PE\' = \'Prince Edward Island\',
\'QC\' = \'Quebec\',
\'SK\' = \'Saskatchewan\',
\'YT\' = \'Yukon\');

$stateList[\'US\'] = array(
\'AL\' = \'Alabama\',
\'AK\' = \'Alaska\',
\'AZ\' = \'Arizona\',
\'AR\' = \'Arkansas\',
\'CA\' = \'California\',
\'CO\' = \'Colorado\',
\'CT\' = \'Connecticut\',
\'DE\' = \'Delaware\',
\'DC\' = \'District of Columbia\',
\'FL\' = \'Florida\',
\'GA\' = \'Georgia\',
\'HI\' = \'Hawaii\',
\'ID\' = \'Idaho\',
\'IL\' = \'Illinois\',
\'IN\' = \'Indiana\',
\'IA\' = \'Iowa\',
\'KS\' = \'Kansas\',
\'KY\' = \'Kentucky\',
\'LA\' = \'Louisiana\',
\'ME\' = \'Maine\',
\'MD\' = \'Maryland\',
\'MA\' = \'Massachusetts\',
\'MI\' = \'Michigan\',
\'MN\' = \'Minnesota\',
\'MS\' = \'Mississippi\',
\'MO\' = \'Missouri\',
\'MT\' = \'Montana\',
\'NE\' = \'Nebraska\',
\'NV\' = \'Nevada\',
\'NH\' = \'New Hampshire\',
\'NJ\' = \'New Jersey\',
\'NM\' = \'New Mexico\',
\'NY\' = \'New York\',
\'NC\' = \'North Carolina\',
\'ND\' = \'North Dakota\',
\'OH\' = \'Ohio\',
\'OK\' = \'Oklahoma\',
\'OR\' = \'Oregon\',
\'PA\' = \'Pennsylvania\',
\'RI\' = \'Rhode Island\',
\'SC\' = \'South Carolina\',
\'SD\' = \'South Dakota\',
\'TN\' = \'Tennessee\',
\'TX\' = \'Texas\',
\'UT\' = \'Utah\',
\'VT\' = \'Vermont\',
\'VA\' = \'Virginia\',
\'WA\' = \'Washington\',
\'WV\' = \'West Virginia\',
\'WI\' = \'Wisconsin\',
\'WY\' = \'Wyoming\');

...

select name=\"state\" id=\"state\" ?php echo arrayToSelect($stateList,\'\',$countryList) ? /select
select name=\"country\" id=\"country\" ?php echo arrayToSelect($countryList,\'US\') ? /select

本文链接: http://array.immuno-online.com/view-747769.html

发布于 : 2021-03-25 阅读(0)
公司介绍
品牌分类
联络我们
服务热线:4000-520-616
(限工作日9:00-18:00)
QQ :1570468124
手机:18915418616