By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,721 Members | 1,206 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,721 IT Pros & Developers. It's quick & easy.

Beginner with a question about associative arrays

P: n/a
Is it possible to reference an index of an associative array using a variable? e.g.: If I had an array:

$myArray["first"] = "myValue";

and if I were to declare a variable:

$myVariable = "first";

I can't get PHP to display "myValue" by referencing this index using $myArray[$myVariable];

Jul 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Fri, 15 Oct 2004 01:41:40 GMT, "chimi" <ti*********@rocketmail.com>
wrote:
Is it possible to reference an index of an associative array using a variable? e.g.: If I had an array:

$myArray["first"] = "myValue";

and if I were to declare a variable:

$myVariable = "first";

I can't get PHP to display "myValue" by referencing this index using $myArray[$myVariable];

Can you post your failed code?

I've just written some code that looks something like this (that works):
echo $foo[str_replace('^', '', $bar)];
The point of me mentioning this is that you can use a variety of code
within the assoc. arrays brackets, a variable is definitely one of them:
$foo['bar'] = 'baz';
$bar = 'bar';
echo $foo[$bar];
This would output:
baz
If you can post your code, we should be able to assist more easily =)

Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Jul 17 '05 #2

P: n/a
Yup. That's the way it works.

<?php
$myArray["first"] = "myValue";
$myVariable = "first";
print $myArray[$myVariable];
?>

This outputs: myValue. So either something's seriously wrong with your
PHP setup, or this isn't the actual code you're trying. When something
obvious isn't working, that's the first sign you're not looking at what
you think you're looking at.

--cd
Jul 17 '05 #3

P: n/a
"chimi" <ti*********@rocketmail.com> wrote in message news:<oVFbd.128848$He1.3709@attbi_s01>...
Is it possible to reference an index of an associative array using a variable? e.g.: If I had an array:

$myArray["first"] = "myValue";

and if I were to declare a variable:

$myVariable = "first";

I can't get PHP to display "myValue" by referencing this index using $myArray[$myVariable];


$myArray["first"] = "myValue" doesn't automatically construct an array.

use

$myArray = array();

as a first line and it works

micha
Jul 17 '05 #4

P: n/a
Ken
ch*********@web.de (chotiwallah) wrote in message >
$myArray["first"] = "myValue" doesn't automatically construct an array.

use

$myArray = array();


Actually,
<?
$myArray['xyz'] = 'any value';
print_r($myArray);
?>

Will create the array fine. The print_r() shows this:
Array
(
[xyz] => any value
)

Ken
Jul 17 '05 #5

P: n/a
On 15 Oct 2004 10:02:52 -0700, ke******@rbnsn.com (Ken) wrote:
ch*********@web.de (chotiwallah) wrote in message >
$myArray["first"] = "myValue" doesn't automatically construct an array.

use

$myArray = array();


Actually,
<?
$myArray['xyz'] = 'any value';
print_r($myArray);
?>

Will create the array fine. The print_r() shows this:
Array
(
[xyz] => any value
)

Ken

This works AFAIK.. but would fail is using array_push() for example.

IMO, not predefining an array is a sloppy coding style and is highly
likely to cause problems later on down the line (PHP5 I would imagine
would baulk on the above example too, although I haven't tried v5 to
back this up)


Regards,

Ian

--
Ian.H
digiServ Network
London, UK
http://digiserv.net/
Jul 17 '05 #6

P: n/a
.oO(chotiwallah)
$myArray["first"] = "myValue" doesn't automatically construct an array.


It does.

$foo['bar'] = 42;
print_r($foo);

outputs

Array
(
[bar] => 42
)

Micha
Jul 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.