2012-09-02 16 views
-1

我需要通過一個多維數組和走路和檢查只有標題如果不與字母開始,爲後續:遍歷數組和應用的preg_match

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

    [1] => Array 
     (
      [letter] => " 
      [id] => 175 
     ) 
.....etc 

,所以我需要檢查只信如果不是用-ZA-Z開始,我已經盡力做到這一點,但還是有一些失蹤,,

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

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

回答

1

請參見下面的網址我認爲這是非常全面幫助你。

更新:

Using preg_match on a multidimensional array to return key values arrays

試試吧

<?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 = "https://stackoverflow.com/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>'; 
?> 
+0

這在以陣列圖案的情形中,我有不同的情況下,我已固定模式(/^[A-ZA-Z] $ /),這我想在多維數組applay ,其中有項目['title','id'],問題是如何才能應用該模式的只有標題,請嘗試再次閱讀我的問題 –

+0

我有更新我的答案目前請看看。看到這個網址http://stackoverflow.com/questions/6281700/using-preg-match-on-a-multidimensional-array-to-return-key-values-array –

1

我刪除了一個foreach循環,改變你的preg_match模式,去除串/行的開始和結束字符串/線錨。

這是我做的:

// 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']; 
    } 
} 

然而,這很可能是有人用正則表達式的更多經驗,可以讓你更好的模式。 :)

http://codepad.viper-7.com/dvUQoW