SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Updating array with array_push is not working

    Hi.

    I am trying to either set an array or update the current array that is held within a session - for some reason it isnt working and is throwing a error (Warning: array_push() expects parameter 1 to be array, integer given in C:\xampp\htdocs\site\folder\cart.php on line 41)

    This is the code

    PHP Code:
    if(empty($_SESSION['cart']))
                {
                    echo 
    "empty session <br>";
                    
    $cartData = array('itemId' => $articleId'itemTitle' => $articleTitle);
                    
    $_SESSION['cart'] = $cartData;
                }
            else
                {
                    echo 
    "full session <br>";
                    
    $cartData $_SESSION['cart'];
                    
    $cartData array_push($cartData,$articleId,$articleTitle);
                    
    $_SESSION['cart'] =$cartData;
                }
                
                    echo 
    "The END: <br>";
                    echo 
    "<pre>";
                    
    print_r($_SESSION['cart']);
                    echo 
    "</pre>";
            
            
            
            
    //echo "<pre>";
            //echo $where . "<br>";
            //print_r($_SESSION['cart']);
            //echo "</pre>"; 
    so far its very simple, I am checking to see if the session is set and if not I create the inital array and assign it to the session there after I should just be pushing into the array and updating the session but as said it throwing the above error.

    What am I doing wrong!

    Cheers
    Chris

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,271
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chrisio View Post
    $cartData = array_push($cartData,$articleId,$articleTitle);
    array_push doesn't return the array. It modifies the array and returns the number of new elements.

  3. #3
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,076
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Try this
    PHP Code:
    if(empty($_SESSION['cart']))
    {
        
    $_SESSION['cart'] = array();
        echo 
    "empty session <br>";
    }
    $item = array('itemId' => $articleId'itemTitle' => $articleTitle);
    array_push($_SESSION['cart'], $item); // alternative: $_SESSION['cart'][] = $item;

    echo "The END: <br>";
    echo 
    "<pre>";
    print_r($_SESSION['cart']);
    echo 
    "</pre>"
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  4. #4
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    276
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    array_push doesn't return the array. It modifies the array and returns the number of new elements.
    Oops... I had even read the manual and missed that!

    Next question then, How do I keep the named index like 'itemId' => $articleId, 'itemTitle' => $articleTitle as array_push doesnt like that syntax or does it not allow it all.. from my reading I cant find away.

    Thanks again
    Chris

  5. #5
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,271
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chrisio View Post
    How do I keep the named index like 'itemId' => $articleId, 'itemTitle' => $articleTitle...
    As is, the keyed values should remain intact, and array_push will add the new items using numeric indexes.


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
  •