Referencing an array?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Referencing an array?

Post by webcan »

Hi there ...

I'm wondering how I can do this in PHP...

If I create an array such as this:

$dow[1] = "Monday";
$dow[2] = "Tuesday";
...

And I have a SQL database which contains the number 1 in another array, $row["id"], how can I get it to reference Monday?

I tried $dow[$row["name"]] but that doesn't work.

Thanks,
Peter.
User avatar
infolock
DevNet Resident
Posts: 1708
Joined: Wed Sep 25, 2002 7:47 pm

Post by infolock »

$dow[1] = "Monday";
$dow[2] = "Tuesday";

And I have a SQL database which contains the number 1 in another array, $row["id"], how can I get it to reference Monday?
sorry, didn't quite follow what you are asking, but is this what you are trying to do ?

Code: Select all

<?php

if ($row["id"] = 1)
{
   echo $dow[1];
}

?>
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

No... it's more like:

Variables:

$dow[1] = "Monday";
$row["id"] = 1;

Long way:

$temp = $row["id"];
$this = $dow[$temp];

What's the short way? :)
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post by mchaggis »

simply:

Code: Select all

$this = $dow&#1111;$row&#1111;"id"]];
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

Well aren't I clever.

You were right... that is correct, and it's what I initially thought (first email), it was coming up blank because I forgot to declare $dow globally.

:-X

THANKS!
Post Reply