array_push使用数组问题

时间:2015-12-31 03:49:11

标签: php array-push

我正在使用foreach和preg_match收集一些数据,如果preg_match为true,它会将该元素添加到数组$ found中,但会返回此错误:

  

PHP警告:array_push()期望参数1为数组,字符串在upfiles.php第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);

感谢您的平常支持

1 个答案:

答案 0 :(得分: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