...

View Full Version : How do you read in a multidimensional array?



anotherJEK
02-29-2012, 11:01 PM
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


// 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.


// $_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

Fou-Lu
02-29-2012, 11:29 PM
Wut?


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.


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

anotherJEK
02-29-2012, 11:48 PM
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


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

anotherJEK
03-01-2012, 01:57 AM
I got it working as:


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.

Fou-Lu
03-01-2012, 05:03 AM
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

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_EOL, print_r(C::$_localImgDirs, true));

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

printf('$b: %s' . PHP_EOL, print_r($b, true));


Results in:


C::$_localImgDirs: Array
(
[one] => 1
[two] => Array
(
[one] => 1
[two] => 2
)

)

$b: Array
(
[one] => 1
[two] => Array
(
[one] => 1
[two] => 2
)

)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum