如果键或键的一部分匹配,则获取关联数组的值


Get the value of an associative array if the key or part of the key is matched

我有一个array,比如

$temp = array('sep', 'oct');

我有一个月份数组,其中包含作为key的月份名称和作为value 的月份编号

$monthsArray = array('January' => 01, 'February' => 02, 'March' => 03 //....so on);

我希望如果$temp的值与$monthsArray中的key匹配(OR包含),则输出其值

在上述情况下,它应该是output 0910

有线索吗?

感谢

您可以尝试这样的函数

function getMonth($temp){
  global $mothsArray;
  foreach ($monthsArray as $key => $value) {
      foreach($temp as $check)
         if ( strpos($key,$check)!== FALSE) {
             return $value;
         }
  } 
  return false;
}

我很确定你可以做得更好,但你可以从这个开始

$search = array('first' => 1, 'second' => 2, 'third' => 3);
$temp = array('sec', 'th');
for($i=0; $i<count($temp); $i++){
    foreach($search as $key => $value){
        if(strpos($key,$temp[$i])!== FALSE){
            echo $value . ' ';
        }
    }
}