4

I have an array that is structured as such:

$data = array(
    "abc"=>array(
            "label" => "abc",
            "value" => "def",
            "type" => "ghi",
            "desc" => "jkl",
            ),
    "def"=>array(
            "label" => "mno",
            "value" => "qrs",
            "type" => "tuv",
            "desc" => "wxyz",
            ),
    );

I want to use preg_match with a foreach loop to perform a search on the arrays contained in $data and return the nested arrays of key value pairs.

1
  • 1
    What is your question? It is possible what you describe, are you asking for a better solution? Commented Jun 8, 2011 at 16:07

7 Answers 7

16

for the googlers out there here's the better code

$data = <as above>
$pattern = "/whatever/";

$matches = array_filter($data, function($a) use($pattern)  {
    return preg_grep($pattern, $a);
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I had never used either array_filter or preg_grep. * mumbles * I'm off to (re)write some code now...
This does the same as the accepted answer. The only difference I can see with the accepted answer is the option to break out of the loop at any time. Both answers returns the whole nested array that has a matching value.
7

Something like this?

<?php
$data = array(
    "abc"=>array(
            "label" => "abc",
            "value" => "def",
            "type" => "ghi",
            "desc" => "jkl",
            ),
    "def"=>array(
            "label" => "mno",
            "value" => "qrs",
            "type" => "tuv",
            "desc" => "wxyz",
            ),
    );

$matches = array();
$pattern = "/a/i";  //contains an 'a'
//loop through the data
foreach($data as $key=>$value){
    //loop through each key under data sub array
    foreach($value as $key2=>$value2){
        //check for match.
        if(preg_match($pattern, $value2)){
            //add to matches array.
            $matches[$key]=$value;
            //match found, so break from foreach
            break;
        }
    }
}
echo '<pre>'.print_r($matches, true).'</pre>';
?>

Comments

1

If you are using PHP 5.5 and are viewing this question in 2015, this might be a more simple answer:

$elements= array_column($array, 1); //Where 1 is the name of the column or the index
$foundElements = preg_grep("/regex/i", $elements);

Comments

1

None of the answers above worked on my case, so I'll leave my solution here maybe it can be useful to someone

function multidimensionalArrayScan($arr, $pattern, &$result = []) : Array
    {

        foreach ($arr as $key => $value) {

            if (is_array($arr[$key])) {
                self::multidimensionalArrayScan($arr[$key], $pattern, $result);
                continue;
            }

            $match  = preg_match($pattern, $value);
            if (!empty($match))
                $result[$key] = $value;
        }

        return $result;
    }

$data = <as above>
$pattern = "/whatever/";
multidimensionalArrayScan($data, $pattern);

Comments

0
$c=['abccd','123','12qw']; // where u'll search
$a = Array('/a/i', '/\d/i', '/\d+\w/i'); // what is
$b = array_map(function($a,$c){return (preg_match_all($a,$c,$m))? ($m[0][0]) : '';}, $a,$c);
print_r($b);

1 Comment

Somebody dares to explain?
0
preg_grep('/needle/iu', array_column($haystack, "columnName", "keyName" ) );

Case-insensitive unicode grep on column columnName in array $haystack

Comments

0

preg_match_all returns the rows that match a pattern with a specific column name.

$pattern = "/REDUCTION/i";
$reductions = array_filter($data, function($a) use($pattern)  {
    return preg_match_all($pattern, $a['budget']);
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.