duanliyi5997 2015-12-30 19:49
浏览 83
已采纳

array_push使用数组问题

I am collecting some data using foreach and preg_match, if preg_match is true it will add that element to the array $found, but returns this error:

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

EDIT:

Get the array_push values into my 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);

Thanks for your usual support

展开全部

  • 写回答

1条回答 默认 最新

  • doupian6118 2015-12-30 19:52
    关注

    if no match is found, $found gets changed to an empty string. what you should do is:

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

    simply remove the else.

    Also what i would do is simply

    $found[]=$vvals;
    

    no real need for array_push

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部