如何将这些数组转换为函数?我无法在网上找到有关如何操作的信息。
$stateArray = array(
"ct"=>"Connecticut",
"ma"=>"Massachusetts",
"nj"=>"New Jersey",
"ny"=>"New York",
"ri"=>"Rhode Island");
$destinationArray = array(
"easterncaribbean"=>"Eastern Caribbean",
"southerncaribbean"=>"Southern Caribbean",
"westerncaribbean"=>"Western Caribbean",
"bermuda"=>"Bermuda",
"bahamas"=>"Bahamas");
答案 0 :(得分:1)
function states() {
return array(
"ct"=>"Connecticut",
"ma"=>"Massachusetts",
"nj"=>"New Jersey",
"ny"=>"New York",
"ri"=>"Rhode Island"
);
}
function destinations() {
return array(
"easterncaribbean"=>"Eastern Caribbean",
"southerncaribbean"=>"Southern Caribbean",
"westerncaribbean"=>"Western Caribbean",
"bermuda"=>"Bermuda",
"bahamas"=>"Bahamas"
);
}
$states = states();
$destinations = destinations();
这是你追求的吗?功能只是可循环使用的代码片段,因此请务必记住这一点。
注意:您的问题没有意义。
答案 1 :(得分:0)
创建返回数组的函数:
// function to return State array
function getStateArray() {
$stateArray = array(
"ct"=>"Connecticut",
"ma"=>"Massachusetts",
"nj"=>"New Jersey",
"ny"=>"New York",
"ri"=>"Rhode Island"
);
return $stateArray;
}
// function to return Destination array
function getDestinationArray() {
$destinationArray = array(
"easterncaribbean"=>"Eastern Caribbean",
"southerncaribbean"=>"Southern Caribbean",
"westerncaribbean"=>"Western Caribbean",
"bermuda"=>"Bermuda",
"bahamas"=>"Bahamas"
);
return $destinationArray;
}
你可以这样调用这些函数:
// call function to get State array
$state = getStateArray();
// this print "Connecticut"
echo "state ct: " . state['ct'];
// call function to get Destination array
$destination = getDestinationArray();
// this print "Eastern Caribbean"
echo "destination easterncaribbean: " . destination['easterncaribbean'];