Problem with Arrays

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
User avatar
Eugene
Forum Newbie
Posts: 24
Joined: Tue Jun 18, 2002 12:40 pm
Location: Montreal

Problem with Arrays

Post by Eugene »

Hi

I declare an array like this

Code: Select all

$week_day_array = array(1 => "every Monday",2 => "every Tuesday",3 => "every Wednesday",4 => "every Thursday",5 => "every Friday",6 => "every Saturday",7 => "every Sunday");
then when I try to access it klike this

Code: Select all

echo $week_day_arrayї1];
echo $week_day_arrayї2];
print_r(array_values ($week_day_array));
nothing is returned from any of these statements. Can anyone help me please????

Thanks
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

Seems to be working for me?
Image Image
User avatar
Eugene
Forum Newbie
Posts: 24
Joined: Tue Jun 18, 2002 12:40 pm
Location: Montreal

Post by Eugene »

Can the array be declared within the same else statement where it is accessed??????

Or do I need to do

Code: Select all

week_day_arrayї'1']
or can the ECHO statement be used with arrays (I guess it should, as the array content are strings)?????
rainman
Forum Newbie
Posts: 2
Joined: Thu Jul 03, 2003 11:46 pm

Re: Problem with Arrays

Post by rainman »

Associative arrays works different to normal number indexed arrays.
If you want to print out the keys and values from the array, try the following code

<?php

$week_day_array = array(1 => "every Monday",
2 => "every Tuesday",
3 => "every Wednesday",
4 => "every Thursday",
5 => "every Friday",
6 => "every Saturday",
7 => "every Sunday");

foreach($week_day_array as $key=>$value) {
echo $key." - ".$value."<br>";
}
?>
That should give you outputs like
1 - every Monday
2 - every Tuesday
3 - every Wednesday
4 - every Thursday
5 - every Friday
6 - every Saturday
7 - every Sunday

:P
Post Reply