Re: Displaying an array value in two columns [message #185851 is a reply to message #185849] |
Mon, 12 May 2014 19:49 |
Christoph Michael Bec
Messages: 207 Registered: June 2013
Karma:
|
Senior Member |
|
|
Dan Jacobs wrote:
> I am fairly new to PHP and have an interesting question concerning the
> proper way to pass an array value from one division to another.
> I have an array I am building "on the fly" so to say. Let's say it has
> 100 items in it. Inititally, there is only one dimension to the array.
> Then later, I add a second dimension to that mainly to save time from
> having to create a second array.
Actually, PHP doesn't have multidimensional arrays; you can only
simulate them with arrays of arrays, so a "two-dimensional" array
consists of n+1 arrays, where n is the number of elements of the first
dimension.
If performance is *really* a concern, you're most likely better off to
use two separate arrays.
> As an example, I have created $myarray[0]. On the second dimension I add
> $myarray[0]['name'].
> Here comes the problem. $myarray[0] prints and displays just fine in
> column 1 and column 2. $myarray[0]['name'] prints fine in column 1, but
> when printed in column 2, I get an illegal offset error message. Is
> there something I am missing to get $myarray[0]['name'] to print in
> column 2?
The "second dimension" will overwrite whatever was stored in
$myarray[0], unless that was already an array in which case you'll add
or overwrite the element with the key "name". You would need to save
the original value of $myarray[0] in a new element, say "value", e.g.
$myarray[0]['value'] = $myarray[0];
$myarray[0]['name'] = $name;
However, that is a clumsy solution. It might be good, if you can
provide some code sample, as Ben already suggested.
--
Christoph M. Becker
|
|
|