duanliyi5997 2015-12-31 03: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-31 03: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

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

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本