-1

我试图创建一个函数,它接受输入,确定其值,并从脚本中包含的现有数组中输出一个单词。问题是输出是空白的,我相信函数会忽略脚本中已经存在的变量,有没有办法改变这个,所以现有的变量不会被函数忽略?

这是功能:

由于多语言要求,单词需要来自数组。

function get_genre($id)
{
    if($id == "1"){
        $genre = $lang['277'];
    }
    if($id == "2"){
        $genre = $lang['278'];
    }
    if($id == "3"){
        $genre = $lang['279'];
    }
    if($id == "4"){
        $genre = $lang['280'];
    }
    if($id == "5"){
        $genre = $lang['281'];
    }
    if($id == "6"){
        $genre = $lang['282'];
    }
    if($id == "7"){
        $genre = $lang['283'];
    }
    if($id == "8"){
        $genre = $lang['284'];
    }
    if($id == "9"){
        $genre = $lang['285'];
    }
    if($id == "10"){
        $genre = $lang['286'];
    }
    if($id == "11"){
        $genre = $lang['287'];
    }
    if($id == "12"){
        $genre = $lang['288'];
    }
    if($id == "13"){
        $genre = $lang['289'];
    }
    if($id == "14"){
        $genre = $lang['290'];
    } 
    if($id == "15"){
        $genre = $lang['374'];
    }
    return $genre;
}    
4

3 回答 3

0
function get_genre($id)
{
    global $lang;
    ....
}

或者

function get_genre($id, $lang) //Must pass $lang array to function here
{

}
于 2012-06-23T02:15:00.353 回答
0
function get_genre($id)
{
 global $lang;

if($id == "1"){
    $genre = $lang['277'];
}
if($id == "2"){
    $genre = $lang['278'];
}
if($id == "3"){
    $genre = $lang['279'];
}
if($id == "4"){
    $genre = $lang['280'];
}
if($id == "5"){
    $genre = $lang['281'];
}
if($id == "6"){
    $genre = $lang['282'];
}
if($id == "7"){
    $genre = $lang['283'];
}
if($id == "8"){
    $genre = $lang['284'];
}
if($id == "9"){
    $genre = $lang['285'];
}
if($id == "10"){
    $genre = $lang['286'];
}
if($id == "11"){
    $genre = $lang['287'];
}
if($id == "12"){
    $genre = $lang['288'];
}
if($id == "13"){
    $genre = $lang['289'];
}
if($id == "14"){
    $genre = $lang['290'];
} 
if($id == "15"){
    $genre = $lang['374'];
}
return $genre;

}

于 2012-06-23T02:15:08.973 回答
-1

虽然这并不理想,但您是否考虑过使用关联数组?

var $lookupArray = array();
$lookupArray["1"] = $lang['274'];
....

然后你可以这样称呼它:

function get_genre($id)
{            

   return(array_key_exists($id,$lookupArray)) ? $lookupArray[$id] : null;                      

} 
于 2012-06-23T02:14:42.420 回答