Search code examples
phparrayssubstringkeytext-extraction

Get numeric suffix from key starting with specific substring


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?


Solution

  • 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...
      }
    }