Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 循环遍历数组并应用preg_匹配_Php - Fatal编程技术网

Php 循环遍历数组并应用preg_匹配

Php 循环遍历数组并应用preg_匹配,php,Php,我需要遍历多维数组,如果标题不是以字母开头,则只检查标题,如下所示: Array ( [0] => Array ( [letter] => [id] => 176 ) [1] => Array ( [letter] => " [id] => 175 ) .....etc 所以

我需要遍历多维数组,如果标题不是以字母开头,则只检查标题,如下所示:

Array
(
    [0] => Array
        (
            [letter] =>  
            [id] => 176
        )

    [1] => Array
        (
            [letter] => "
            [id] => 175
        )
.....etc  
所以我只需要检查字母,如果不是以a-zA-z开头,我已经试过了,但仍然缺少一些东西

$notMatch = array();
foreach ($data as $value) {
    foreach ($value as $item['title']=>$d) {

       if(!preg_match('/^[a-zA-Z]$/',$d)){
           $notMatch[]=$d;
       }
    }
}

请看下面的网址,我认为这对你很有帮助

更新:

试试看

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

我删除了一个foreach循环并更改了预匹配模式,删除了字符串/线的开头和字符串/线的结尾锚

我就是这样做的:

然而,很有可能拥有更多正则表达式经验的人可以为您提供更好的模式。:)


对于数组中的模式,我有不同的情况,我有固定的模式(/^[a-zA-Z]$/),我想在多维数组中应用,其中有项目['title','id',],问题是如何仅对标题应用模式,请再次尝试阅读我的问题。我现在更新了我的答案,请查看。看到这个网址了吗
// I'm assuming your data array looks something like this:
$data = array(array('title'=>'fjsdoijsdiojsd', 'id'=>3),
                    array('title'=>'oijijsd', 'id'=>5),
                    array('title'=>'09234032', 'id'=>3));

$notMatch = array();
foreach ($data as $value) {
   if(!preg_match('/([a-zA-Z]).*/',$value['title'])){
       $notMatch[]=$value['title'];
       echo 'notmatch! ' . $value['title'];
   }
}