selecting an array key based on partial string

sea_1987 picture sea_1987 · Oct 14, 2010 · Viewed 40.3k times · Source

I have an array and in that array I have an array key that looks like, show_me_160 this array key may change a little, so sometimes the page may load and the array key maybe show_me_120, I want to now is possible to just string match the array key up until the last _ so that I can check what the value is after the last underscore?

Answer

oezi picture oezi · Oct 14, 2010

one solution i can think of:

foreach($myarray as $key=>$value){
  if("show_me_" == substr($key,0,8)){
    $number = substr($key,strrpos($key,'_'));
    // do whatever you need to with $number...
  }
}