vinpkl
September 27, 2011, 5:26am
1
hi all
$days = array("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday");
foreach($days as $key => $value){
echo $key ." ". $value."<br>";
}
The above outputs
0 monday
1 tuesday
2 wednesday
3 thursday
4 friday
5 saturday
6 sunday
$date = date("l");
echo $date;
The outputs day as “tuesday” and its correct.
now if “tuesday” is found in array values, then i want to output the key position.
if($date == $value){
echo "<br> today's key position is ".$key;
}
this above code outputs as 6. It should be 1.
vineet
system
September 27, 2011, 6:13am
2
[COLOR=#000000]echo $date [COLOR=#007700];
[/COLOR][/COLOR]outputs “Tuesday”
vinpkl
September 27, 2011, 6:17am
3
hi webdev
if i change my arrays values to “Tuesday” with a capital T, then also it doesnt works
$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
foreach($days as $key => $value){
echo $key ." ". $value."<br>";
}
vineet
Try array_search() which will give key for what is being searched for or false if what is being searched for is not found.
vinpkl
September 27, 2011, 6:28am
5
hi spacephoenix
i did get the value with array_search()
but i cant get the value displayed with my “if statement”
$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
foreach($days as $key => $value){
echo $key ." ". $value."<br>";
}
$date = date("l");
echo $date;
echo "<br><br>";
$keyv = array_search($date, $days);
echo "<br> today's key value is ".$keyv. "<br>";
if($date == $value){
echo "<br> today's key position is ".$keyv;
}
what am i doing wrong
vineet
The if statement is comparing $date which has a value of Tuesday with $value which has a value of Sunday. When the value of $date is Sunday then the if statement will evaluate to true. You can check this by running the script then changing your system time to a Sunday and running the script again (don’t forget to set your system time back again afterwards).
vinpkl
September 27, 2011, 6:52am
7
hi Spacephoenix
thats where i m stuck. why am i geting the value as 6 or sunday.
Do i need to split the array values and then match with the current day value.
vineet
system
September 27, 2011, 6:52am
8
<?php
$days = array("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday");
$date = date("l");
foreach($days as $key => $value){
if($value == $date){
echo 'key = '.$key; //outputs 1
}
}
?>
Uhm, any specific reason not to use date(‘w’) or date(‘N’) here?
foreach($days as $key => $value){
echo $key ." ". $value."<br>";
}
Think about it. Each time the loop goes around, it puts a new value into $value. What would be sitting in $value when it’s done?
Hint: What would the value be if I made your array:
array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday','Snurzleberryday');
?
vinpkl
September 27, 2011, 12:54pm
12
hi michael
i took date() just as an example.
i m in learning stage and was stuck with using “if” statement with “foreach” loop in arrays.
webdev gave me a good solution.
vineet