0

I'm trying to learn PHP and challenged myself with this simple demo to learn how to use arrays. The challenge: display a string that will show a restaurant's daily special for the appropriate day.

Sunday = closed
Monday = Taco
Tuesday = Chicken
Wednesday = Lasagna
Thursday = Sushi
Friday = Salmon
Saturday = Steak

Example if the current day is Thursday:
Today's special is Sushi.

Here's the baseline I've been working from:

<?php
date_default_timezone_set('America/New_York');
$day = date('l');
$days = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
$specials = array('closed','Taco','Chicken','Lasagna','Sushi','Salmon','Steak');
?>
<p>Today is <?php echo $day; ?></p>
<p>Today's Special is: <?php Cant Figure Out What Goes Here ?></p>
4
  • You would have to use a for loop and many if statements. I can help you. Commented Aug 31, 2017 at 20:30
  • Look up associative array Where it's $key => $value. Assign key as day and value as specials. Loop with a foreach($arrayname as $key => $special) { if($day == $key)} echo $special {; Commented Aug 31, 2017 at 20:30
  • 2
    Tip: date('w') and use that as the array index ($days[date('w')]). Commented Aug 31, 2017 at 20:32
  • Possible duplicate of Is this code is correct to print time according to days There is no shortage of "[php] lookup array" pages on SO. Always research and research some more before posting a question. Commented Aug 31, 2017 at 21:51

3 Answers 3

2

You can use like this

<p>Today is <?php echo $day; ?></p>
<p>Today's Special is: <?php echo $specials[array_search($day, $days)]; ?></p>

array_search will get the key of the provided value($day) from $days array.

$specials[array_search($day, $days)] will get the value from $specials array.

Sign up to request clarification or add additional context in comments.

2 Comments

Today is Thursday but some reason the code is displaying Sunday's special (i.e. It's closed)
1

You could do it this way

$current_day = date('l');

//set up your array so you can grab the special based on the key (Which is the day)
$days = array(
    //key "Sunday" value "Closed"
    "Sunday" => "Closed", 
    //key "Monday" value "Taco" etc etc etc
    "Monday" => "Taco",
    "Tuesday" => "Chicken", 
    "Wednesday" => "Lasagna",
    "Thursday" => "Sushi", 
    "Friday" => "Salmon",
    "Saturday" => "Steak",
);

//check which day it is using a switch statement
switch($current_day) {
    //if it's sunday, you should display a "we are closed" message
    case "Sunday":
        $special = "Sorry, today we are closed.";
        break;
    //if its anything but sunday, access the array using the key, which happens to be a `$current_day` every time
    default:
        $special = "Todays special is: {$days[$current_day]}";

}

//echo current day
echo "<p>Today is {$current_day}</p>";

//echo $special based on the switch statement above.
echo "<p>{$special}</p>";

Don't get confused by my use of curly brackets, those are called complex expressions, I just explained how they work just a few moments ago. They are just for string concatenation. You could echo those variables the same way as you did before as well

<p>Today is <?php echo $current_day; ?></p>
<p><?php $special; ?></p>

2 Comments

Awesome! Thank you! I also appreciate the comments in your example to help me understand the logic so I can learn from it.
@ScottS. That's my goal!:) If you have any questions, don't hesitate to ask
0

You can do this simply by iterating through the variables in the arrays. The first time of the loop, it will print Sorry, we are closed. The rest of the time, it will print (2nd time): Today's special is Taco!The .is used to join things up, like the + in other languages.

<html>
  <body>
    <?php
        date_default_timezone_set('America/New_York');
        $day = date('l');
        $days = array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
        $specials = array('closed','Taco','Chicken','Lasagna','Sushi','Salmon','Steak');

        for($i=0;$i<$days;$i++){
            if($days[$i] == "Monday"){

                echo $day;
                echo "Sorry we are".$specials[$i];

            }

            echo $day;
            echo "Today's special is".$specials[$i]."!"

        }

    ?>

  </body>
</html>

I hope this solves you doubt. So what I did work, your file must be saved as .php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.