Page 1 of 1

Problem with Arrays

Posted: Wed Jul 02, 2003 11:02 am
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

Posted: Wed Jul 02, 2003 11:05 am
by phice
Seems to be working for me?

Posted: Wed Jul 02, 2003 11:16 am
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)?????

Re: Problem with Arrays

Posted: Fri Jul 04, 2003 12:01 am
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