Comparing array obtained to data from database PHP -


i junior programmer still learning how write code. have array in php

//data inside array days name $days = array(); for($date = $from_date; $date <= $to_date; $date->modify('+1 day')) {     array_push($days,strtolower($date->format('l'))); } 

from array, there list days has been selected user (monday,tuesday,etc)

then have table in database

work_scheme 

the work_scheme consists of table

//field_name => data monday => working day tuesday => working day wednesday => working day thursday => working day friday => working day saturday => half day sunday => off day 

this working_days array data retrieved database

$working_days = array(); if(count($work_scheme) > 0){         foreach($work_scheme $r){             $working_days[0] = array(                 "monday" => $r['monday']             );             $working_days[1] = array(                 "tuesday" => $r['tuesday']             );               $working_days[2] = array(                 "wednesday" => $r['wednesday']             );                   $working_days[3] = array(                 "thursday" => $r['thursday']             );               $working_days[4] = array(                 "friday" => $r['friday']             );               $working_days[5] = array(                 "saturday" => $r['saturday']             );               $working_days[6] = array(                 "sunday" => $r['sunday']             );         }     } 

so how can compare array obtained user's activity table in database?

i have following code doesn't work properly

for($i = 0; $i < count($days); $i++){         for($x = 0; $x < count($working_days); $x++){             $total_days = 0;             if($days[$i] == $working_days[$x]){                 echo "hello world";             }         }     } 

i notice $working_days[$x] won't return me day name, instead return me either working day, half day, or off day how can compare days name $days() $working_days day?

so if let's chosen days friday, saturday, , sunday, how can write codes return me 1.5 days?

working day = 1  half day  = 0.5 off day = 0 

try this:

first, initialise working days array in more useful way

$scheme_days = ["monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday"]; $working_days = []; if (count($work_scheme) > 0) {     foreach ($work_scheme $r)     {         foreach ($scheme_days $scheme_day)         {             $working_days[$scheme_day] = $r[$scheme_day];         }     } } 

then, calculate days:

$total_days = 0; ($i = 0; $i < count($days); $i++) {     $worked_day = strtolower($working_days[strtolower($days[$i])]);      if ($worked_day == "working day")     {         $total_days++;     }     else if ($worked_day == "half day")     {         $total_days += 0.5;     } }  echo $total_days; 

Comments

Popular posts from this blog

jOOQ update returning clause with Oracle -

java - Warning equals/hashCode on @Data annotation lombok with inheritance -

java - BasicPathUsageException: Cannot join to attribute of basic type -