Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    How do you read in a multidimensional array?

    I am trying to load a multidimensional array into an object property.
    I have found that, in this context, the object array property cannot be literally
    set to a provided array argument
    PHP Code:
    // this works in a __constructor 
     
    foreach($_imageDirs as $this->ind=>$this->val)
                {
                   
    self::$_localImgDirs[$this->ind] = $this->val;
                }
    // whereas (both are arrays)
    self::$_localImgDirs $_imageDirs // does not work 
    So, I have been trying to read in a multidimensional array in the
    __construct method.
    PHP Code:
    // $_imgArrayList is an associative array of associative arrays
    // which exists in an external, require_once('file')
    foreach($_imgArrayList as $this->ind=>$this->val)
               {
                
    self::$_localImgLists[$this->ind] = array();
                foreach(
    $_imgArrayList[$this->ind] as $this->inda=>$this->vala)
                    {
                      
    self::$_localImgLists[$this->ind][$this->inda] = $this->vala;
                    }
                } 
    I have been trying various versions, without success:
    Any suggestions?
    Thanks for time and attention
    JK

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Wut?
    PHP Code:
    self::$_localImgLists $_imgArrayList
    Just assign it over.
    Also, its somewhat bizarre to use a foreach and call the $key => $value pairs from an object instance member since they typically carry no worth on their own. Just use local variables instead.

    Edit:
    You have two class level variables here too. I'm not sure which you want to use, _localImgLists or _localImageDirs.

  • Users who have thanked Fou-Lu for this post:

    anotherJEK (02-29-2012)

  • #3
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post

    O.k.....

    As for 'edit'; $_localImgDirs is an example of a single dimensional array that exists in this code. I have tried direct assignment and I cannot get it to work.
    When I call a method to produced what is supposed to be there I get
    ... invalid argument used in foreach.....

    Direct assignment of multidimensional arrays also will not produce the expected result
    PHP Code:
    public function getContentMarkup($_local$_choice// subhead, item
             
    {
                 
    //...... code cut
                   
    $this->local $_local;
                   
    $this->choice $_choice;
                   
    $this->imgList self::$_localImgLists[$this->local];
                   
    $this->testOut ''// for test purposes
                   
    foreach($this->imgList as $this->ind=>$this->val// line 419 in error below
                      
    {
                       
    $this->testOut .= $this->ind.' : '.$this->val."<br />\n";
                      }
                   return  
    $this->testOut
              }
    //Warning: Invalid argument supplied for foreach() in /<path>/whitecloud/WC_php/pixViewerApp.php on line 419 

  • #4
    Regular Coder
    Join Date
    Aug 2010
    Location
    Now Southern Oregon. I was born and had lived my life in Los Angeles until relocating last year (2010)
    Posts
    215
    Thanks
    52
    Thanked 1 Time in 1 Post
    I got it working as:
    PHP Code:
    foreach($_imgArrayList as $this->ind=>$this->val)
               {
                 
    self::$_localimgLists[$this->ind] = $this->val;
               } 
    Part of the problem was that the array of arrays was defined before the arrays it
    is supposed to contain, in the configuration file. So The values didn't get assigned
    in that file. The other problems appeared to be misspellings. But direct assignment
    still does not seem to work in this context.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    I still haven't a clue what you are talking about. Direct assignment is what you need, so that would indicate your usage is incorrect.
    For an example:
    PHP Code:
    <?php

    class C
    {
        public static 
    $_localImgDirs = array(); // Instantiation isn't actually required.

        
    public static function aFunc(array $in)
        {
            
    self::$_localImgDirs $in;
        }
    }

    $a = array('one' => 1'two' => array('one' => 1'two' => 2));

    C::aFunc($a);
    printf('C::$_localImgDirs: %s' PHP_EOLprint_r(C::$_localImgDirstrue));

    $b = array();
    // Below is the same as $b = $a;
    foreach ($a AS $key => $val)
    {
        
    $b[$key] = $val;
    }

    printf('$b: %s' PHP_EOLprint_r($btrue));
    Results in:
    Code:
    C::$_localImgDirs: Array
    (
        [one] => 1
        [two] => Array
            (
                [one] => 1
                [two] => 2
            )
    
    )
    
    $b: Array
    (
        [one] => 1
        [two] => Array
            (
                [one] => 1
                [two] => 2
            )
    
    )


  •  

    Posting Permissions

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