SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Warning: array_push(): First argument should be an array

    Hi all!

    I'm trying to insert some posted form elements into an array with array_push, but I'm getting this warning.

    My code:

    PHP Code:
    $parr = array();

    if(isset(
    $_POST['btnsubmit']))
    {
        foreach(
    $_POST as $key => $val)
        {
            if( (
    $key != 'funcionario') && ($key != 'btnsubmit') && ($key != 'btnreset') )
            {
                
    array_push($parr[$key], $val);
            }
        }
        echo 
    '<pre>';
        
    print_r($parr);
        echo 
    '</pre>';

    What am I doing wrong?
    I defined $parr as an array before looping through the $_POST array...

    Thank you.
    Yours truely
    Mário Ramos

  2. #2
    SitePoint Evangelist -T-'s Avatar
    Join Date
    Jun 2002
    Posts
    444
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $parr = new array();
    chrome is a wrapper that combines a browser with spyware

  3. #3
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    If I try that line of code, I get:

    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'$''
    Yours truely
    Mário Ramos

  4. #4
    SitePoint Guru
    Join Date
    Feb 2008
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The error is being generated because $parr[$key], is not an array, $parr is.

    So your either need to use:
    array_push($parr, $val);

    Or you might be better with
    $parr[$key] = $val;

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    63 Post(s)
    Tagged
    0 Thread(s)
    You could also just unset the keys that you don't want.
    PHP Code:
    if(isset($_POST['btnsubmit']))
    {
        
    $parr $_POST;
        unset(
    $parr['funcionario'], $parr['btnsubmit'], $parr['btnreset']);
        echo 
    '<pre>'print_r($parrTRUE), '</pre>';

    Salathe
    Software Developer and PHP Manual Author.

  6. #6
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all your replies.

    My code now:

    PHP Code:
    foreach($_POST as $key => $val)
    {
        if( (
    $key != 'funcionario') && ($key != 'btnsubmit') && ($key != 'btnreset') )
        {
            
    $parr[$key] = $val;
        }
    }

    echo 
    '<pre>';
    print_r($parr);
    echo 
    '</pre>'
    Yours truely
    Mário Ramos

  7. #7
    SitePoint Zealot
    Join Date
    Feb 2005
    Location
    Aveiro, Portugal
    Posts
    164
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Salathe,

    I'll try your suggestion.
    Looks good too.

    Yours truely
    Mário Ramos


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •