1

for example I am iterating class slots using foreach like

foreach ($result as $timeslot) {
       $time=strtotime($timeslot);
       $endTime = date("h:i A", strtotime('+' . $duration . ' minutes', $time));
       echo date('h:i', strtotime($timeslot)) . '- ' . date('h:i A', strtotime($endTime));

}

I have another array foreach which iterates over booked_slot like

foreach($booked_slot as $slot){
         $slot=strtotime($slot['start_time']);
        echo "slot<br>";
       }

The result of var_dump for $booked_slot is like:

[0]=>
  object(app\modules\admin\models\OrderItem)#195 (10) {
    ["_attributes":"yii\db\BaseActiveRecord":private]=>
    array(10) {
      ["id"]=>
      int(128)
      ["user_id"]=>
      int(73)
      ["location_id"]=>
      int(2)
      ["instructor_id"]=>
      int(16)
      ["order_id"]=>
      int(54)
      ["cd_id"]=>
      int(7)
      ["price"]=>
      string(6) "300.00"
      ["date"]=>
      string(10) "2018-04-12"
      ["start_time"]=>
      string(8) "10:45:00"
      ["end_time"]=>
      string(8) "10:55:00"
    }

and the result of var_dump of $result is like

    array(14) {
  [0]=>
  string(8) "10:05:00"
  [1]=>
  string(8) "10:15:00"
  [2]=>
  string(8) "10:25:00"
  [3]=>
  string(8) "10:35:00"
  [4]=>
  string(8) "10:45:00"
  [5]=>
  string(8) "10:55:00"
  [6]=>
  string(8) "11:05:00"
  [7]=>


}

What I want is if $result have matching values for $booked_slot[0]['start_time'] then that value is removed from the iteration of $result

To achieve this one option I tried is like

if(strtotime($timeslot) != $slot)

this works partly as where there is only one value in $result it works find, but if it has multiple values it works for only the last one.

The queries to generate the object array is like this:

$class_duration = ClassDuration::find()->where(['instructor_id' => $values['id']])->andwhere($cond)->all();
$booked_slot = OrderItem::find()->where(['instructor_id' => $values['id'],'date' =>$datec])->andwhere($cond)->all();

2 Answers 2

1

In your foreach() around $booked_slot, you are overriding the $slot value each time of iteration. So, only the last value is stored.

You could create an array to store all values, and use in_array() to check if $timeslot is inside:

$slots = [];
foreach($booked_slot as $slot){
    $slots[] = strtotime($slot['start_time']);
}

And to check:

if (!in_array(strtotime($timeslot), $slots)) {
    // $timeslot is not in $slots.
}
Sign up to request clarification or add additional context in comments.

1 Comment

That is perfect. I am getting the result I was looking for. Thanks a lot.
1

The easiest way to become this is to put all booked slots in an array so you can check if your timeslot exists in this array.

<?php
$slot_array = array();
foreach($booked_slot as $slot){
    $slot_array[]=strtotime($slot['start_time']);
    echo "slot<br>";
}
?>

AND

<?php
    if(!in_array(strtotime($timeslot), $slot_array)){
       // not booked yet
    }
?>

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.