25

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 key matches a pattern? I will know the value of the first digit in the key,but not the second. so for example, using a prefix of "2.", I want to somehow be able to find the key "2.5" and return both the key and the value "ABDE".

I was thinking about using a regular expression with a pattern like:

$prefix = 2;
$pattern = '/'.$prefix.'\.\d/i';

and then looping through the array and checking each key. (by the way, just for demo purposes, $prefix has been hardcoded to 2, but in the real system, this is a value provided by the user's input).

I'm wondering if there's a simpler way to do this?

Thanks.

dot
  • 14,928
  • 41
  • 110
  • 218
  • if the prefix is dependent on user input, it can be anything, right? – Teena Thomas Sep 18 '12 at 17:59
  • Can the user enter a.a, a.* or is the user limited to 2.*, 3.*, *.5 (numbers)? And if they are limited to numbers are they limited to the tenths place? And finally are the keys ([2.5]) doubles like they appear to be or are they strings? – Gabe Spradlin Sep 18 '12 at 18:22

7 Answers7

62

I think you need something like this:

$keys = array_keys($array);
$result = preg_grep($pattern, $keys);

The result will be a array that holds all the keys that match the regex. The keys can be used to retrieve the corresponding value.

Have a look at the preg_grep function.

JvdBerg
  • 21,777
  • 8
  • 38
  • 55
20

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

Ibu
  • 42,752
  • 13
  • 76
  • 103
  • 1
    is there any way to avoid looping? – dot Sep 18 '12 at 18:04
  • 3
    this is not the right anwer, the right one can be found lower down, JvdBerg has the right one. There's a function in PHP specifically for finding matching keys in an array – patrick Aug 31 '16 at 11:34
13

Old question, but here's what I like to do:

$array = [ '2.5' => 'ABDE', '4.8' => 'Some other value' ];

preg_grep + array_keys will find all keys

$keys = preg_grep( '/^2\.\d/i', array_keys( $array ) );

You can add array_intersect_key and array_flip to extract a slice of the array that matches the pattern

$vals = array_intersect_key( $array, array_flip( preg_grep( '/^2\.\d/i', array_keys( $array ) ) ) );
Nick B
  • 487
  • 4
  • 8
2

For future programmers who encounter the same issue. Here is a more complete solution which doesn't use any loops.

    $array = ['2.5'=> 'ABCDE', '2.9'=>'QWERTY'];
    $keys = array_keys($array);
    $matchingKeys = preg_grep('/^2\.+/', $keys);
    $filteredArray = array_intersect_key($array, array_flip($matchingKeys));
    print_r($filteredArray);
Turdaliev Nursultan
  • 2,538
  • 1
  • 22
  • 28
1

That are my ways

$data = ["path"=>"folder","filename"=>"folder/file.txt","required"=>false];

// FIRST WAY

$keys = array_keys($data);
if (!in_array("path", $keys) && !in_array("filename",$keys) && !in_array("required",$keys)) {
    return myReturn(false, "Dados pendentes");
}

// SECOND WAY

$keys = implode("," array_keys($data));
if (!preg_match('/(path)|(filename)|(required)/'), $keys) {
    return myReturn(false, "Dados pendentes");
}
Marcelo
  • 1,486
  • 13
  • 16
0

To get just the part of the array with matching keys you could also write

$matching_array = array_flip(preg_grep($pattern, array_flip($your_array)));

This might just lead to problems performance-wise, if your array gets too big.

0

There's T-Regx library for regular expression in PHP, and it kas preg::grep_keys() method.

<?php
$array = [2.5 => 'ABDE', 4.8 => 'Some other value'];

preg::grep_keys("/$prefix\.\d/i", $array);
Danon
  • 2,771
  • 27
  • 37