www.webdeveloper.com
Results 1 to 3 of 3

Thread: Trying to initialise an array but getting: syntax error, unexpected T_VARIABLE

  1. #1
    Join Date
    Dec 2013
    Location
    Melbourne, Australia
    Posts
    26

    Trying to initialise an array but getting: syntax error, unexpected T_VARIABLE

    I'm trying to use array_push to create an array that looks like this:

    Code:
    array(
      "SaleTypeCat1" => array("Type1", Type2","Type3"),
      "SaleTypeCat2" => array("Type3")
    )
    My code fails on this line:

    PHP Code:
    $this->valid_sale_types_categories[$row->sale_category] = array(); 
    with:

    PHP Parse error: syntax error, unexpected T_VARIABLE
    This is my code - From what I have seen and read, I have followed the instructions to initialise my array however, the error suggests I'm not doing it correctly?

    PHP Code:
    class SaleTypes {

        private 
    $valid_sale_types_categories;
            
        function 
    getSaleTypes($customer_id) { 
            
            ...
                
            while (
    $row $result->fetch_object()) {
                
                    
                
    //Need to initialise the array before trying to push a value to it.
                
    if (!isset($this->valid_sale_types_categories[$row->sale_category]) 
                    
    $this->valid_sale_types_categories[$row->sale_category] = array();
                        
                
    array_push($this->valid_sale_types_categories[$row->sale_category], $row->sale_type);
            }

            ...

        }

    If I try to just push the array before initializing I get a different error:

    PHP Warning: array_push() [<a href='function.array-push'>function.array-push</a>]: First argument should be an array

  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,331
    You are missing a closing parenthesis on this line:
    Code:
    if (!isset($this->valid_sale_types_categories[$row->sale_category]))
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Dec 2013
    Location
    Melbourne, Australia
    Posts
    26

    resolved

    OMG! That's embarrassing...

    Thanks for your feedback!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles