2016-11-05 184 views
-1

如果您創建一個數組並使用函數preg_match_all填充值,那麼如何從數組中檢索每個單獨的值(基本上是從索引0到整個長度的掃描)?我想獲取每個值並在其上執行另一個函數,那麼如何返回存儲在數組中的每個值? (我想改編的索引爲0值,在索引1等)從PHP中的數組中檢索值

$contents = file_get_contents('words.txt'); 
$arr = array(); 
preg_match_all($pattern, $contents, $arr); //finds all matches 
$curr = current($arr); 

我試着這樣做(我的模式是在別的地方寫的),後來呼應,但我不斷收到字符串「陣列」。

+0

我想拿起,除塵,並回答這個問題。你能否提供(作爲你的問題的一個編輯)一個最小的,相關的'$ contents'可以包含的樣本?你能解釋一下你想要捕捉哪些部分嗎?有了這些新信息,我可以嘗試爲您提供解決方案。更新問題後請ping我。 – mickmackusa

回答

-1
$contents=file_get_contents('words.txt'); 
$arr=array(); 
preg_match_all($pattern,$contents,$arr);//finds all matches 
foreach ($arr as $item) { 
    $curr=current($arr); 
    // do something 
} 
+0

感謝您在foreach循環中的提示,但是我仍然在回顯時發現$ curr是「Array」。它應該是返回「陣列」? – li127

-1

preg_match_all()至少需要參數:(1)正則表達式。 (2)匹配你的正則表達式的字符串。它還需要第三個可選參數,它會自動填充所有找到的匹配項。這第三個參數是一個數字索引數組。因此,您可以像循環陣列一樣循環,並像對待任何正常陣列(,它是)那樣對待它。下面的代碼片段演示這個使用代碼:

<?php 

    $contents = file_get_contents('words.txt'); 
    //$arr = array(); //<== NO NEED TO PRE-DECLARE THIS HERE 

    preg_match_all($pattern, $contents, $arr);//finds all matches 

    // IF YOU JUST WANT THE 1ST (CURRENT MATCH), SIMPLY USE current: 
    $curr  = current($arr); 
    // YOU MAY KEEP MOVING THE CURSOR TO THE NEXT ITEM LIKE SO: 
    $next1 = next($arr);  
    $next2 = next($arr); 

    // OR JUST LOOP THROUGH THE FOUND MATCHES: $arr 
    foreach($arr as $match){ 
     $curr = current($arr); //<== BUT THIS IS SOMEWHAT REDUNDANT WITHIN THIS LOOP. 

     // THE VARIABLE $match CONTAINS THE MATCHED STRING WITHIN THE CURRENT ITERATION: 
     var_dump($match);  //<== RETURNS THE CURRENT VALUE WITHIN ITERATION 
    } 

    // YOU MAY EVEN USE NUMERIC INDEXES TO ACCESS THEM LIKE SO. 
    $arrLen = count($arr); 
    $elem0 = isset($arr[0])?$arr[0]:null; 
    $elem1 = isset($arr[1])?$arr[1]:null; 
    $elem2 = isset($arr[2])?$arr[2]:null; 
    $elem3 = isset($arr[3])?$arr[3]:null; //<== AND SO ON... 
-1

如果我沒有理解好你的問題,你想知道如何查找存儲在preg_match_all第三個參數的結果。

preg_match_all將結果作爲二維數組存儲在第三個參數中。

兩個結構是可能的,並且可以在第四個參數中用兩個常量顯式設置。

比方說,你有兩個拍攝組的模式和你的主題字符串包含3次出現的模式:

1)PREG_PATTERN_ORDER是返回類似的東西默認設置:

[ 
    0 => [ 0 => 'whole match 1', 
      1 => 'whole match 2', 
      2 => 'whole match 3' ], 

    1 => [ 0 => 'capture group 1 from match 1', 
      1 => 'capture group 1 from match 2', 
      2 => 'capture group 1 from match 3' ], 

    2 => [ 0 => 'capture group 2 from match 1', 
      1 => 'capture group 2 from match 2', 
      2 => 'capture group 2 from match 3' ] 
] 

2 )PREG_SET_ORDER返回類似的東西:

[ 
    0 => [ 0 => 'whole match 1', 
      1 => 'capture group 1 from match 1', 
      2 => 'capture group 2 from match 1' ], 

    1 => [ 0 => 'whole match 2', 
      1 => 'capture group 1 from match 2', 
      2 => 'capture group 2 from match 2' ], 

    2 => [ 0 => 'whole match 3', 
      1 => 'capture group 1 from match 3', 
      2 => 'capture group 2 from match 3' ] 
] 

具體的例子可以在找到。你只需要選擇哪一個更適合你需要做的選擇。因此,要應用您的功能,您只需執行foreach循環或使用array_map(這也是一個隱藏循環,但更短更慢)。因此,要應用您的功能,您只需執行foreach循環或使用array_map即可。

一般來說,如果您不確定變量的結構是什麼,您可以使用print_rvar_dump來了解它的外觀。