Array php not displaying key with same value

Asked

Viewed 79 times

2

I have an array of hours(s) and an array of Schedule(s). When a customer makes an appointment, this time goes to the agenda array. 2 Customers scored the 08hrs, 3 clients the 09hrs ... same as this in the agenda array. Then I want to take the hours when an hour reaches the attendance limit. For example: If I determine that at 9:00 am only 3 calls can be made, in the next hour listing this 9:00 am time cannot appear anymore and this is working perfectly up to that point. But if you repeat two quantities of queries at different times it only takes one random and needed to remove the two with the same amount of queries marked.

Example:

08:00 = 2 scheduled queries;

09:00 = 3 scheduled queries;

but if 10:00 also has 3 queries scheduled he withdraws either the 09hrs or 10hrs and I need to withdraw both if repeat the amount determined by me. Someone could help me?

Follows the code:

// horários que são exibidos para os clientes
$hours        = array('08:00','08:30','09:00','09:30','10:00','10:30','11:00','11:30','12:00','12:30','13:00','13:30','14:00');

// horários que os clientes marcaram
$schedule     = array('08:00','08:00','09:00','09:00','09:00','10:00','10:00','10:00','10:00','11:00','11:00','11:00','11:00','11:00' );

// ao chegar nesse valor de consultas agendada por horário é retirado o horário do array hours
$chooseNumber = 5;

$count        = array_count_values( $schedule );
foreach( $count as $number => $value )
{

    if( $value > 1 )
    {
        $schedules[$value] = $number;

        if( $value == $chooseNumber )
        {
             $newHours[$value] = $number;
        }

     }


}
echo "<pre>";
print_r( $schedules );
echo "</pre>";

echo "<pre>";
print_r( array_diff( $hours, $newHours ) );
echo "</pre>";
  • sorry for the way I posted it. I’m still not used to the stack way to ask questions. But it might help me in this code?

  • how do I put as solved?

2 answers

1


I would do so:

(code is commented)

// ao chegar nesse valor de consultas agendada por horário é retirado o horário do array hours
$chooseNumber = 3;

$count        = array_count_values( $schedule );
$array_com_horarios_livres = array();

foreach($hours as $hour){
    # SE O HORÁRIO EM $hours ESTIVERM EM $count E A QUANTIDADE FOR MAIOR OU IGUAL AO LIMITE CONTINUE O LOOP SEM INSERIR
    if(isset($count[$hour]) && $count[$hour] >= $chooseNumber)
        continue;
    $array_com_horarios_livres[] = $hour; # INSERE NO NOVO ARRAY COM OS HORÁRIOS LIVRES
}

print_r($array_com_horarios_livres);

See it working on Ideone

  • Thank you very much, perfect!

  • @Thiagomoreira does not forget to approve please!

  • how do I approve?

  • @Thiagomoreira in the left corner at the top of the reply has a sign of (up) and (down) for you to evaluate the answer... Below this has a sign for you to mark as the right answer.

  • 1

    Thank you, approved!!

0

The problem is in this part:

if( $value == $chooseNumber )
    {
         $newHours[$value] = $number;
    }

because the moment you make the newHours array and put it in $value, if you have two with the same value ex: 9:00,9:00,11:00,11:00 will be placed 9 the first time after as value will be the same before the 11 will replace in the same position. as you know exactly the position you need to take is only inside your if you put an unset

if( $value == $chooseNumber )
    {
        unset($hours[$number]);
    }

and in the end you just

print_r($hours)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.