Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #13612 > unrolled thread

Displaying an array value in two columns

Started by"Dan Jacobs" <Dan.Jacobs@gmal.com>
First post2014-05-12 15:15 -0400
Last post2014-05-12 22:00 +0200
Articles 4 — 4 participants

Back to article view | Back to comp.lang.php


Contents

  Displaying  an array value in two columns "Dan Jacobs" <Dan.Jacobs@gmal.com> - 2014-05-12 15:15 -0400
    Re: Displaying  an array value in two columns Ben Bacarisse <ben.usenet@bsb.me.uk> - 2014-05-12 20:27 +0100
    Re: Displaying  an array value in two columns Christoph Michael Becker <cmbecker69@arcor.de> - 2014-05-12 21:49 +0200
    Re: Displaying  an array value in two columns "J.O. Aho" <user@example.net> - 2014-05-12 22:00 +0200

#13612 — Displaying an array value in two columns

From"Dan Jacobs" <Dan.Jacobs@gmal.com>
Date2014-05-12 15:15 -0400
SubjectDisplaying an array value in two columns
Message-ID<Qq-dnTmQ3KWtg-zOnZ2dnUVZ_oidnZ2d@giganews.com>
Hello all.

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

Thanks for any kind help.

Dan.

[toc] | [next] | [standalone]


#13613

FromBen Bacarisse <ben.usenet@bsb.me.uk>
Date2014-05-12 20:27 +0100
Message-ID<0.df79f498f1d42794aa77.20140512202755BST.87egzyu79g.fsf@bsb.me.uk>
In reply to#13612
"Dan Jacobs" <Dan.Jacobs@gmal.com> writes:

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

That sounds like an odd thing to do, and not a very good reason to do
it!  How much time does it save?

> 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?

You'll have to post the code, or code that exhibits the same problem.
I, for one, can't work out what you are doing (let alone what you are
doing wrong) from this description.

-- 
Ben.

[toc] | [prev] | [next] | [standalone]


#13614

FromChristoph Michael Becker <cmbecker69@arcor.de>
Date2014-05-12 21:49 +0200
Message-ID<537125b7$0$6713$9b4e6d93@newsspool2.arcor-online.net>
In reply to#13612
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

[toc] | [prev] | [next] | [standalone]


#13615

From"J.O. Aho" <user@example.net>
Date2014-05-12 22:00 +0200
Message-ID<btcnj2FcvfeU1@mid.individual.net>
In reply to#13612
On 12/05/14 21:15, Dan Jacobs wrote:
> Hello all.
>
> 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.
> 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?

As Ben already explained, we need the code to be able to help you.
Could it be that you think your array is multidimensional but it ain't 
in the all cases.


You can try to use var_export() to see how the array looks like

syslog(LOG_INFO, var_export($myarray, true));

then look in the /var/log/messages (or where your system logs syslog 
messages) to see how the array looked like.


You could also use a function like this (far from perfect) to display 
the array:

function printArray($yourArray) {
         foreach($yourArray as $value) {
                 if(is_array($value)) {
                         print "\n";
                         printArray($value);
                 } else {
                         print $value ." | ";
                 }
         }
         print "\n";
}


example:
$myarray = array( "something",
"something else",
array("name" => "Someone", "Age" => "50"),
array("name" => "Zimone", "Age" => "12", "Color" => "Blue")
);
printArray($myarray);

output something like:
something | something else |
Someone | 50 |

Zimone | 12 | Blue |

-- 

  //Aho

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web