php - finding keys in an array that match a pattern

前端 未结 6 604
刺人心
刺人心 2020-12-10 11:13

I have an array that looks like:

 Array ( [2.5] => ABDE [4.8] => Some other value ) 

How would I find any key/value pair where the k

6条回答
  •  误落风尘
    2020-12-10 11:42

    you can simply loop through the array and check the keys

    $array = array(...your values...);
    
    foreach($array as $key => $value) {
        if (preg_match($pattern,$key)){
            // it matches
        }
    }
    

    You can wrap it in a function and pass your pattern as parameter

提交回复
热议问题