#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0

    Populating an array variable within a class


    Hello group.

    I have the following class....

    PHP Code:
    class Storm
    {
      public 
    $StormName;
      public 
    $StormID;
      public 
    $Latitude = array();  //used to hold a list of positions
      
    public $Longitude = array(); //same as above

    When I want to add values to my class I've been trying the following, but my arrays are not being populated.
    I get an error message ... array_push first argument should be an array....

    PHP Code:
    $aStorm = new Storm();
    $aStorm->StormName 'test';  //works!
    $aStorm->StormID '4';          //works! 
    array_push($aStorm->Latitude'23.4'); //does not work
    array_push($aStorm->Longitude'75.4'); //does not work 
    Please help with the correct way to populate the array.
    Many thanks!
    Bryan
  2. #2
  3. No Profile Picture
    I haz teh codez!
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2003
    Posts
    2,549
    Rep Power
    2337
    Whoops, you gave the error, I scrolled past it.

    It works for me:

    php Code:
    <?php
     
    class Storm
    {
      public $StormName;
      public $StormID;
      public $Latitude = array();  //used to hold a list of positions
      public $Longitude = array(); //same as above
    }  
     
    $aStorm = new Storm();
    $aStorm->StormName = 'test';  //works!
    $aStorm->StormID = '4';          //works! 
    array_push($aStorm->Latitude, '23.4'); //does not work
    array_push($aStorm->Longitude, '75.4'); //does not work  
     
    print_r($aStorm);


    Result:
    Code:
    php -f storm.php 
    Storm Object
    (
        [StormName] => test
        [StormID] => 4
        [Latitude] => Array
            (
                [0] => 23.4
            )
    
        [Longitude] => Array
            (
                [0] => 75.4
            )
    
    )
    What version of PHP is this?
    I ♥ ManiacDan & requinix

    This is a sig, and not necessarily a comment on the OP:
    Please don't be a help vampire!
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    10
    Rep Power
    0
    Originally Posted by ptr2void
    Whoops, you gave the error, I scrolled past it.

    It works for me:

    php Code:
    <?php
     
    class Storm
    {
      public $StormName;
      public $StormID;
      public $Latitude = array();  //used to hold a list of positions
      public $Longitude = array(); //same as above
    }  
     
    $aStorm = new Storm();
    $aStorm->StormName = 'test';  //works!
    $aStorm->StormID = '4';          //works! 
    array_push($aStorm->Latitude, '23.4'); //does not work
    array_push($aStorm->Longitude, '75.4'); //does not work  
     
    print_r($aStorm);


    Result:
    Code:
    php -f storm.php 
    Storm Object
    (
        [StormName] => test
        [StormID] => 4
        [Latitude] => Array
            (
                [0] => 23.4
            )
    
        [Longitude] => Array
            (
                [0] => 75.4
            )
    
    )
    What version of PHP is this?
    I feel so stupid.... I misspelled a word (even though I spelled it correctly in the quick example above...) I've spent no less than 3 hours trying to figure this out and it comes down to something so simple....
    Thanks for your help and clarity!
    Regards,
    Bryan

IMN logo majestic logo threadwatch logo seochat tools logo