0

I am trying to store values in a for loop as an array so I can access them outside them outside the loop.

foreach ($doctor->booking as $booking) {
    $bookeddate = date('Y-m-d', strtotime($booking->booked_time));

    if( $bookeddate == $end_date ) {
        $booked_time[] = date('H:i a', strtotime($booking->booked_time));
    }
}

foreach ($booked_time as $key ) {
    echo $key;
}

This code keeps giving me an error "Undefined variable: booked_time"

3
  • probably because you never pass this test if($bookeddate==$end_date){. Be sure to initialize the var as an empty array if you want to avoid that, or test with isset Commented Oct 2, 2018 at 14:00
  • 1
    It could also be that $booked_time was never created because the if statement conditions were never met? Commented Oct 2, 2018 at 14:00
  • if($bookeddate==$end_date) if this condition doesn't true then $booked_time will be undefined Commented Oct 2, 2018 at 14:00

6 Answers 6

2

try initializing $booked_time before using as it has scope inside function only

$booked_time = [];
foreach ($doctor->booking as $booking) {
       $bookeddate=date('Y-m-d',strtotime($booking->booked_time));
           if($bookeddate==$end_date){
             $booked_time[]=date('H:i a',strtotime($booking->booked_time));
           }
 }

 foreach ($booked_time as $key ) {
      echo $key;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you so much that was just it.
1

This is not applicable...

The array you were created is valid only for that scope(here it is for your FOREACh).

As soon as you are get out from that scope,the array variable is diappeared. SOLUTION - Declare your array in a global scope where both two foreach can be accessed.

1 Comment

PHP's scopes don't work in that way. Declaring a variable inside a for loop will not prevent it from being used later on.
1

Three things you should do:

  1. initialize the variable $booked_time as empty array before looping: then you are sure to always have an array defined before the loop, as in: $booked_time = []; - this will make the error disappear

  2. verify $end_date actually has a value (the definition is not included in your code snippet) so there is something to compare

  3. if it does have a value, ensure $end_date is formatted as date("Y-m-d") just like the bookeddate because you are doing a string comparison, where date("Y-m-d") is not the same as date("Ymd") even though they reference the exact same day

Comments

0

This is not possible because of the concept of block scope. You can read more about variable scoping in PHP here.

Generally, this means, that every variable declared within curly braces is only accessible in this block of code.

The easiest way to work around this issue is to create an empty array beforehand, like so:

$booked_time = [];

foreach ($doctor->booking as $booking) {
   $bookeddate=date('Y-m-d',strtotime($booking->booked_time));
       if($bookeddate==$end_date){
         $booked_time[]=date('H:i a',strtotime($booking->booked_time));
       }
 }

 foreach ($booked_time as $key ) {
      echo $key;
 }

2 Comments

In order to access your variable ´$a´ you first need to initialize it. This must happen because otherwise your variable is not defined and in your case, the interpreter wouldn't know, that it is meant to be an array.
0

create array outside the for loop.

then use array_push() to add elements to the array

possible solution

$booked_times = [];

foreach ($doctor->booking as $booking) {
    $bookeddate = date('Y-m-d',strtotime($booking->booked_time));

    if($bookeddate==$end_date){
        array_push($booked_times, date('H:i a',strtotime($booking->booked_time));
    }
}

foreach ($booked_time as $key ) {
    echo $key;
}

Comments

0

You may want to consider using a collection. They are really cool and you have access to so many methods. https://laravel.com/docs/5.7/collections. I'm going to use code from the post you liked above and just change the array to a collection.

$booked_time = collect();
foreach ($doctor->booking as $booking) {
       $bookeddate=date('Y-m-d',strtotime($booking->booked_time));
           if($bookeddate==$end_date){
             $booked_time->push(date('H:i a',strtotime($booking->booked_time)));
           }
 }

 foreach ($booked_time as $key ) {
      echo $key;
 }

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.