好吧,試圖做一個函數,我可以傳遞一個變量,它將搜索靜態當前硬編碼的多維數組中的鍵,並返回匹配到找到的鍵的數組如果找到)。陣列映射函數,每次返回false
這是我到目前爲止。
public function teamTypeMapping($teamType)
{
//we send the keyword football, baseball, other, then we return the array associated with it.
//eg: we send football to this function, it returns an array with nfl, college-football
$mappings = array(
"football" => array('nfl', 'college-football'),
"baseball" => array('mlb', 'college-baseball'),
"basketball" => array('nba', 'college-basketball'),
"hockey" => array('nhl', 'college-hockey'),
);
foreach($mappings as $mapped => $item)
{
if(in_array($teamType, $item)){return $mapped;}
}
return false;
}
而且我想打個電話吧,例如:
teamTypeMapping("football");
AMD都將其返回與關鍵字「足球」相關的陣列,我已經試過這幾種方法,每次我冒出虛假的,也許我錯過了一些東西,所以我現在就採取一些建議。
ü可以用這個也如果($ teamType == $映射){$返回映射;}做 – Dikku
這個'的foreach取代你的foreach( $映射爲$映射=> $項目) { \t \t如果($映射== $ teamType){ \t \t \t返回$映射[$映射]; \t \t} }' –
或者回答@Luke Mills給出的答案 –