根据部分字符串选择数组键 [英] selecting an array key based on partial string

查看:32
本文介绍了根据部分字符串选择数组键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,在该数组中我有一个数组键,看起来像 show_me_160 这个数组键可能会发生一些变化,所以有时页面可能会加载,而数组键可能是 show_me_120,我想现在可以只字符串匹配数组键直到最后一个 _ 以便我可以检查最后一个下划线之后的值是什么?

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?

推荐答案

我能想到的一个解决方案:

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

这篇关于根据部分字符串选择数组键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆