PHP数组,根据值获取密钥

时间:2023-02-12 22:07:11

If I have this array

如果我有这个数组

$england = array(
        'AVN' => 'Avon',
        'BDF' => 'Bedfordshire',
        'BRK' => 'Berkshire',
        'BKM' => 'Buckinghamshire',
        'CAM' => 'Cambridgeshire',
        'CHS' => 'Cheshire'
);

I want to be able to get the three letter code from the full text version, how would i write the following function:

我希望能够从全文版本中获取三字母代码,我将如何编写以下函数:

$text_input = 'Cambridgeshire';
function get_area_code($text_input){
    //cross reference array here
    //fish out the KEY, in this case 'CAM'
    return $area_code;
}

thanks!

谢谢!

2 个解决方案

#1


25  

Use array_search():

使用array_search():

$key = array_search($value, $array);

So, in your code:

所以,在你的代码中:

// returns the key or false if the value hasn't been found.
function get_area_code($text_input) {
    global $england;
    return array_search($england, $text_input);
}

If you want it case insensitive, you can use this function instead of array_search():

如果您希望它不区分大小写,则可以使用此函数而不是array_search():

function array_isearch($haystack, $needle) {
   foreach($haystack as $key => $val) {
       if(strcasecmp($val, $needle) === 0) {
           return $key;
       }
   }
   return false;
}

If you array values are regular expressions, you can use this function:

如果数组值是正则表达式,则可以使用此函数:

function array_pcresearch($haystack, $needle) {
   foreach($haystack as $key => $val) {
       if(preg_match($val, $needle)) {
           return $key;
       }
   }
   return false;
}

In this case you have to ensure all values in your array are valid regular expressions.

在这种情况下,您必须确保数组中的所有值都是有效的正则表达式。

However, if the values are coming from an <input type="select">, there's a better solution: Instead of <option>Cheshire</option> use <option value="CHS">Cheshire</option>. Then the form will submit the specified value instead of the displayed name and you won't have to do any searching in your array; you'll just have to check for isset($england[$text_input]) to ensure a valid code has been sent.

但是,如果值来自,则有一个更好的解决方案:使用

#2


6  

If all values in $england are unique, you can do:

如果$ england中的所有值都是唯一的,您可以执行以下操作:

$search = array_flip($england);
$area_code = $search['Cambridgeshire'];

#1


25  

Use array_search():

使用array_search():

$key = array_search($value, $array);

So, in your code:

所以,在你的代码中:

// returns the key or false if the value hasn't been found.
function get_area_code($text_input) {
    global $england;
    return array_search($england, $text_input);
}

If you want it case insensitive, you can use this function instead of array_search():

如果您希望它不区分大小写,则可以使用此函数而不是array_search():

function array_isearch($haystack, $needle) {
   foreach($haystack as $key => $val) {
       if(strcasecmp($val, $needle) === 0) {
           return $key;
       }
   }
   return false;
}

If you array values are regular expressions, you can use this function:

如果数组值是正则表达式,则可以使用此函数:

function array_pcresearch($haystack, $needle) {
   foreach($haystack as $key => $val) {
       if(preg_match($val, $needle)) {
           return $key;
       }
   }
   return false;
}

In this case you have to ensure all values in your array are valid regular expressions.

在这种情况下,您必须确保数组中的所有值都是有效的正则表达式。

However, if the values are coming from an <input type="select">, there's a better solution: Instead of <option>Cheshire</option> use <option value="CHS">Cheshire</option>. Then the form will submit the specified value instead of the displayed name and you won't have to do any searching in your array; you'll just have to check for isset($england[$text_input]) to ensure a valid code has been sent.

但是,如果值来自,则有一个更好的解决方案:使用

#2


6  

If all values in $england are unique, you can do:

如果$ england中的所有值都是唯一的,您可以执行以下操作:

$search = array_flip($england);
$area_code = $search['Cambridgeshire'];