【问题标题】:array_push using arrays issuearray_push 使用数组问题
【发布时间】:2016-04-05 02:09:37
【问题描述】:

我正在使用 foreach 和 preg_match 收集一些数据,如果 preg_match 为真,它将将该元素添加到数组 $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);

感谢您一直以来的支持

【问题讨论】:

    标签: php array-push


    【解决方案1】:

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

    【讨论】:

    • 哦,是的,else 中的 $found 是一个字符串
    • 我在上面编辑了我的代码,如何在调用类时从数组中获取这些值?
    • 这是一个新问题,应该作为一个问题提出
    • 好的,我会问一个新问题
    猜你喜欢
    • 2015-01-03
    • 1970-01-01
    • 1970-01-01
    • 2012-01-07
    • 2023-03-26
    • 1970-01-01
    • 2020-06-10
    • 2018-08-24
    • 1970-01-01
    相关资源
    最近更新 更多