2013-05-14 37 views
0

林通過$文件陣列,並試圖循環:循環通過數組和字符串把出現的字符串到另一個數組

  1. 查找「一些字符串」的出現。
  2. 對於找到的每個「某些字符串」事件,我需要將它添加到數組中。 (即$ some_strings)。
  3. 最後能夠調用$ some_string陣列的外部for循環操作(即計數(),$ some_string [1])

    foreach($files as $key=>$value) 
    { 
    if(strstr($value,'some-string')){ 
        $some_strings = array(); 
        $some_strings = $files[$key]; 
        unset($files[$key]); 
    } elseif (strstr($value,'php')) { 
        unset($files[$key]); 
        } 
    
    
    } 
    

每個事情似乎做工精細,直到我嘗試計數($ some_strings)。當我知道有至少10個值時,這隻返回1值。我究竟做錯了什麼?

+0

您可能要招行初始化'$ some_strings'到外循環。 – andrewsi 2013-05-14 15:01:32

+0

好的。我會在循環之外移動它。 – codename32 2013-05-14 15:03:03

回答

1

試試這個

$some_strings = array(); 
foreach($files as $key=>$value) 
{ 
    if(strstr($value,'some-string')){ 
     $some_strings[] = $files[$key]; 
     unset($files[$key]); 
    } elseif (strstr($value, 'php')) { 
     unset($files[$key]); 
    } 
} 
//Now you can use $some_strings here without a problem 
0

試試這個

foreach($files as $key=>$value) 
{ 
    if(strstr($value,'some-string')) 
    { 
    $some_strings[$key] = $value; 
     unset($files[$key]); 
    } elseif (strstr($value,'php')) 
    { 
    $another_strings[$key] = $value; 
     unset($files[$key]); 
    } 
} 

echo count($some_strings); 
echo count($another_strings); 
相關問題