473,385 Members | 2,013 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Associative Array In Class

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
Jul 17 '05 #1
4 2274
corey wrote:
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


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>";
}
}
Jul 17 '05 #2
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
corey wrote:
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


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>";
}
}

Jul 17 '05 #3
On 18 Jul 2004 15:20:32 -0700
co**************@yahoo.com (corey) wrote:
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.


I think he meant here:
$this->$row = array("A"=>"Apple", "B"=>"Bob");


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

Jul 17 '05 #4
.oO(corey)
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;
var $row;
function MyClass()
{
$this->$row = array("A"=>"Apple", "B"=>"Bob");
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.
echo "One: " . $this->$row["A"] . "<br>";
echo "Two: " . $this->row["A"] . "<br>";


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
Jul 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Abdullah Kauchali | last post by:
Hi folks, Can one rely on the order of keys inserted into an associative Javascript array? For example: var o = new Object(); o = "Adam"; o = "Eve";
6
by: mark4asp | last post by:
Suppose I have the following code. It functions to randomly select a city based upon the probabilities given by the key differences in the associative array. . Eg. because the key difference...
4
by: Robert | last post by:
I am curious why some people feel that Javascript doesn't have associative arrays. I got these definitions of associative arrays via goggle: Arrays in which the indices may be numbers or...
8
by: Derek Basch | last post by:
Is there any way to associate name/value pairs during an array initialization? Like so: sType = "funFilter" filterTypeInfo = ; filterTypeInfo = new Array("type" : sType); I can do it using...
47
by: VK | last post by:
Or why I just did myArray = "Computers" but myArray.length is showing 0. What a hey? There is a new trend to treat arrays and hashes as they were some variations of the same thing. But they...
7
by: Robert Mark Bram | last post by:
Hi All! How do you get the length of an associative array? var my_cars= new Array() my_cars="Mustang"; my_cars="Station Wagon"; my_cars="SUV"; alert(my_cars.length);
1
by: swayze | last post by:
Hi there, We're using php 4.1 and it doesn't seem to have built in support for this. Coming from a dotnet background this surprised me...Anyways, thats a different topic altogether... I'm...
41
by: Rene Nyffenegger | last post by:
Hello everyone. I am not fluent in JavaScript, so I might overlook the obvious. But in all other programming languages that I know and that have associative arrays, or hashes, the elements in...
13
by: Daniel Klein | last post by:
I have a class constructor that accepts an array as the only argument. The catch is that the array MUST be an 'integer-indexed' array, not an 'associative' array, because the index position has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.