2015-12-31 129 views
0

我收集使用的foreach和的preg_match一些數據,如果的preg_match是真的,那將添加元素,以發現了數組$,但返回此錯誤:array_push使用數組問題

PHP Warning: array_push() expects parameter 1 to be array, string given in upfiles.php line 324

$found = array(); 

    foreach($this->disFunctions as $kkeys => $vvals) 
    {    
     if(preg_match('#'.$vvals.'#i', $cFile)) 
     {     
      array_push($found, $vvals); // LINE 324 
     } else { 
      $found = ''; 
     } 
    } // end foreach 

    print_r($found); 

編輯:

獲取array_push值到類:

public final function filterFile(){ 

    $disabled_functions = ini_get('disable_functions'); 

    $disFunctionsNoSpace = str_replace(' ', '', $disabled_functions); 

    $disFunctions = explode(',', $disFunctionsNoSpace); 

    $this->disFunctions = $disFunctions; 

    // get file content of the uploaded file (renamed NOT the temporary) 
    $cFile = file_get_contents($this->fileDestination, FILE_USE_INCLUDE_PATH); 


    $found = array(); 

    foreach($this->disFunctions as $kkeys => $vvals) 
    {    
     if(preg_match('#'.$vvals.'#i', $cFile)) 
     {     
      array_push($found, $vvals); 

     } 
    } // end foreach 

} // end filterFile 


$up = new uploadFiles($filename); 

$fileterringFile = $up->filterFile(); 

print_r($fileterringFile); 
var_dump($fileterringFile); 

感謝您的平常支持

回答

3

如果找不到匹配項,$found被更改爲空字符串。你應該做的是:

$found = array(); 

foreach($this->disFunctions as $kkeys => $vvals) 
{    
    if(preg_match('#'.$vvals.'#i', $cFile)) 
    {     
     array_push($found, $vvals); // LINE 324 
    } 
} // end foreach 

print_r($found); 

只是刪除其他。

也是我會做的是簡單地

$found[]=$vvals; 

沒有真正的需要array_push

+0

噢,在$在別人發現是一個字符串 –

+0

我編輯我上面的代碼中,我怎樣才能得到這些值從數組中調用一個類? –

+0

這是一個新問題,應該作爲一個問號 –