Comparing an array retrieved with data from a PHP database

I am a junior programmer who is still learning to write code. I have this array in PHP

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

From this array there will be a list for the days selected by the user (Monday, Tuesday, etc.)

Then I have this table in my database

work_scheme

Work_scheme consists of a 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 is my work_days array that is retrieved from the database

$working_days = array();
if(count($work_scheme) > 0){
        foreach($work_scheme as $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 I compare the array obtained from user activity to my table in the database?

I have the following code but it does not 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 that $ working_days [$ x] will not return the day name to me, instead it will return me the working day, half day or day. How can I compare the day name from $ days () to $ work_days?

, - , , , 1,5 ?

Working day = 1 
Half day  = 0.5
Off day = 0
+4
1

- :

.

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

:

$total_days = 0;
for ($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;
0

Source: https://habr.com/ru/post/1649050/


All Articles