在多维数组上使用 preg_match 返回键值数组

发布于 2024-11-15 00:39:04 字数 468 浏览 2 评论 0原文

我有一个结构如下的数组:

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

我想使用 preg_match 和 foreach 循环来对 $data 中包含的数组执行搜索并返回键值对的嵌套数组。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

寄风 2024-11-22 00:39:04

对于谷歌员工来说,这里有更好的代码

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

$matches = array_filter($data, function($a) use($pattern)  {
    return preg_grep($pattern, $a);
});

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);
});
何止钟意 2024-11-22 00:39:04

像这样的东西吗?

<?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>';
?>

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>';
?>
冷弦 2024-11-22 00:39:04

如果您使用 PHP 5.5 并在 2015 年查看此问题,这可能是一个更简单的答案:

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

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);
满身野味 2024-11-22 00:39:04

上面的答案都不适合我的情况,所以我将在这里留下我的解决方案,也许它对某人有用

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);

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);
耳钉梦 2024-11-22 00:39:04
$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);
$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);
分开我的手 2024-11-22 00:39:04
preg_grep('/needle/iu', array_column($haystack, "columnName", "keyName" ) );

数组 $haystack 中的列 columnName 上不区分大小写的 unicode grep

preg_grep('/needle/iu', array_column($haystack, "columnName", "keyName" ) );

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

醉梦枕江山 2024-11-22 00:39:04

preg_match_all 返回与具有特定列名的模式匹配的行。

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

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']);
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文