array_push 使用数组问题

array_push using arrays issue

我正在使用 foreach 和 preg_match 收集一些数据,如果 preg_match 为真,它将将该元素添加到数组 $found,但是 returns 这个错误:

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 值输入我的 class:

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);

感谢您一贯的支持

如果未找到匹配项,$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);

只需删除 else。

另外我会做的只是

$found[]=$vvals;

没必要 array_push