Connecting Tech Pros Worldwide Forums | Help | Site Map

Associative Array In Class

corey
Guest
 
Posts: n/a
#1: Jul 17 '05
In PHP Version 4.3.6 I am trying get a value from a class member that
is an associative array...

class MyClass
{
$var row;

function MyClass()
{
$this->$row = array("A"=>"Apple", "B"=>"Bob");

echo "One: " . $this->$row["A"] . "<br>";
echo "Two: " . $this->row["A"] . "<br>";

$cache = $this->$row;
echo "Three: " . $cache ["A"] . "<br>";
}
}

But this is the output...

One: Array
Two:
Three: Apple

How can I get at the value of the associative array without making a
local copy first!

Corey

neur0maniak
Guest
 
Posts: n/a
#2: Jul 17 '05

re: Associative Array In Class


corey wrote:[color=blue]
> In PHP Version 4.3.6 I am trying get a value from a class member that
> is an associative array...
>
> class MyClass
> {
> $var row;
>
> function MyClass()
> {
> $this->$row = array("A"=>"Apple", "B"=>"Bob");
>
> echo "One: " . $this->$row["A"] . "<br>";
> echo "Two: " . $this->row["A"] . "<br>";
>
> $cache = $this->$row;
> echo "Three: " . $cache ["A"] . "<br>";
> }
> }
>
> But this is the output...
>
> One: Array
> Two:
> Three: Apple
>
> How can I get at the value of the associative array without making a
> local copy first!
>
> Corey[/color]

Don't use a $ after the -> symbol

Try this:
class MyClass
{
$var row;

function MyClass()
{
$this->row = array("A"=>"Apple", "B"=>"Bob");

echo "One: " . $this->row["A"] . "<br>";
echo "Two: " . $this->row["A"] . "<br>";

$cache = $this->row;
echo "Three: " . $cache ["A"] . "<br>";
}
}
corey
Guest
 
Posts: n/a
#3: Jul 17 '05

re: Associative Array In Class


I tried that in the original example... It is what is output after
"Two:" but that ouputs nothing if you look at the output in my first
post.

Corey
[color=blue]
> corey wrote:[color=green]
> > In PHP Version 4.3.6 I am trying get a value from a class member that
> > is an associative array...
> >
> > class MyClass
> > {
> > $var row;
> >
> > function MyClass()
> > {
> > $this->$row = array("A"=>"Apple", "B"=>"Bob");
> >
> > echo "One: " . $this->$row["A"] . "<br>";
> > echo "Two: " . $this->row["A"] . "<br>";
> >
> > $cache = $this->$row;
> > echo "Three: " . $cache ["A"] . "<br>";
> > }
> > }
> >
> > But this is the output...
> >
> > One: Array
> > Two:
> > Three: Apple
> >
> > How can I get at the value of the associative array without making a
> > local copy first!
> >
> > Corey[/color]
>
> Don't use a $ after the -> symbol
>
> Try this:
> class MyClass
> {
> $var row;
>
> function MyClass()
> {
> $this->row = array("A"=>"Apple", "B"=>"Bob");
>
> echo "One: " . $this->row["A"] . "<br>";
> echo "Two: " . $this->row["A"] . "<br>";
>
> $cache = $this->row;
> echo "Three: " . $cache ["A"] . "<br>";
> }
> }[/color]
Anders K. Madsen
Guest
 
Posts: n/a
#4: Jul 17 '05

re: Associative Array In Class


On 18 Jul 2004 15:20:32 -0700
coreydalejohnson@yahoo.com (corey) wrote:
[color=blue]
> I tried that in the original example... It is what is output after
> "Two:" but that ouputs nothing if you look at the output in my first
> post.
>[/color]

I think he meant here:[color=blue][color=green][color=darkred]
> > > $this->$row = array("A"=>"Apple", "B"=>"Bob");[/color][/color][/color]

It should be $this->row = array("A" => "Apple", "B" => "Bob");
The other evaluates to $this->"" since $row is empty.

--
Anders K. Madsen --- http://lillesvin.linux.dk

"There are 10 types of people in the world.
Those who understand binary - and those who don't."

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iD8DBQFA+wCdlNHJe/JASHcRAukpAJ9plLY56vmaiPoba9SGkyfmJlCVqACfUO8r
H99Ds7x5jNRwi5h8EY+VvY0=
=zOEn
-----END PGP SIGNATURE-----

Michael Fesser
Guest
 
Posts: n/a
#5: Jul 17 '05

re: Associative Array In Class


.oO(corey)
[color=blue]
>In PHP Version 4.3.6 I am trying get a value from a class member that
>is an associative array...
>
>class MyClass
>{
> $var row;[/color]

var $row;
[color=blue]
> function MyClass()
> {
> $this->$row = array("A"=>"Apple", "B"=>"Bob");[/color]

The line above creates a new unnamed (name is probably an empty string)
property in your object ($row is not initialized => NULL) and assigns
the array to it.

Set error_reporting to E_ALL and you'll see a notice on that.
[color=blue]
> echo "One: " . $this->$row["A"] . "<br>";
> echo "Two: " . $this->row["A"] . "<br>";[/color]

The first of the lines above accesses this "anonymous" property and
outputs the first element, the second accesses the real and previously
declared property, which is still empty.

In fact

$this->row

and

$this->$row

are completly different things. The first directly accesses a property
named 'row', the second accesses a property, whose name is stored in the
variable $row.

HTH
Micha
Closed Thread