当我知道数组的值时,我怎么知道数组的键


How can I know the key of my array when I know the value?

我有一个多数组:

$bouton["modify-customer"] = array("fr"=>"liste-client", "en"=>"customer-list");
$bouton["create-customer"] = array("fr"=>"creer-client", "en"=>"create-customer");
$bouton["modify-item"] = array("fr"=>"liste-item", "en"=>"item-list");
$bouton["create-item"] = array("fr"=>"creer-item", "en"=>"create-item");
$bouton["modify-taxes"] = array("fr"=>"liste-taxes", "en"=>"taxes-list");
$bouton["create-taxes"] = array("fr"=>"creer-taxes", "en"=>"create-taxes");

在一个页面中,我有这个字符串:">liste-tax">

我需要找到:">税收清单">

我怎样才能完成这项任务?

我知道我需要在这里找到正确的键,它是修改税,然后我可能会找到另一个值,而不是fr值,而是en值。

我知道我不是很清楚,我的英语也不是很好,但我希望你们能帮助我,我会留在网站上,这样我就可以回答你的问题,并在未来的评论中与你交谈。

谢谢。

您需要

遍历数组并搜索值,如下所示:

foreach($bouton as $key => $array){
    if( in_array("liste-taxes",$array)){
        echo $key . PHP_EOL;
        echo $bouton[$key]['en'];
    }
}

输出:

modify-taxes 
taxes-list

只需遍历数组并找到翻译:

$search = "liste-taxes";
$bouton["modify-customer"]      = array("fr"=>"liste-client", "en"=>"customer-list");
$bouton["create-customer"]  = array("fr"=>"creer-client", "en"=>"create-customer");
$bouton["modify-item"]          = array("fr"=>"liste-item", "en"=>"item-list");
$bouton["create-item"]      = array("fr"=>"creer-item", "en"=>"create-item");
$bouton["modify-taxes"]     = array("fr"=>"liste-taxes", "en"=>"taxes-list");
$bouton["create-taxes"]     = array("fr"=>"creer-taxes", "en"=>"create-taxes");
array_walk($bouton, function($v, $i) use($search) {
   if($v['fr'] === $search) {
       echo $v['en'];
   } 
});
array_walk($bouton, function ($val) use ($searched, $lang, &$result) {
    if (in_array($searched, $val))
        $result = $val[$lang];
});

其中$searched是您搜索的字符串,$lang您搜索的语言。 $result将包含最终值。

例:

$bouton["modify-customer"]      = array("fr"=>"liste-client", "en"=>"customer-list");
$bouton["create-customer"]  = array("fr"=>"creer-client", "en"=>"create-customer");
$bouton["modify-item"]          = array("fr"=>"liste-item", "en"=>"item-list");
$bouton["create-item"]      = array("fr"=>"creer-item", "en"=>"create-item");
$bouton["modify-taxes"]     = array("fr"=>"liste-taxes", "en"=>"taxes-list");
$bouton["create-taxes"]     = array("fr"=>"creer-taxes", "en"=>"create-taxes");
$searched = "liste-taxes";
$lang = "en";
array_walk($bouton, function ($val) use ($searched, $lang, &$result) {
    if (in_array($searched, $val))
        $result = $val[$lang];
});
print $result;

你可以建立一个方便的翻译函数。

function translate($array, $searchterm, $lan){
foreach($array as $key => $array){
if( in_array($searchterm,$array)){
    return $array[$lan];
}}}

然后只需将数组、术语和语言传递给它,您将获得FR或EN版本,具体取决于您指定的内容。

  echo translate($bouton,"taxes-list","fr");
foreach($bouton as $key => $array){
    if( in_array("taxes-list",$array)){
        echo $key . PHP_EOL;
        echo $bouton[$key]['en'];
    }
}

Thaks to immulatin, phpisubuer01 和 bwoebi !!工作就像一种魅力。