MMK
MMK

Reputation: 365

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

I've an array Let's say

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

and I've an array of months that contains the month name as key and number of month as the value

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

I want that if $temp value matches with (OR contains) the key in $monthsArray then output it's value

in above case it should output 09 and 10

any clue?

Thanks

Upvotes: 0

Views: 59

Answers (2)

Rivnat
Rivnat

Reputation: 1507

$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 . ' ';
        }
    }
}

Upvotes: 1

Radu Bogdan
Radu Bogdan

Reputation: 504

You could try a function like this

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

I'm pretty sure you can make it better, but you can start from this

Upvotes: 2

Related Questions