Compare two number arrays

Asked

Viewed 158 times

0

I’m having a hard time making a comparison on PHP between two arrays.

I have the Array HORA_INICIAL and HORA_FINAL what I need to do :

In the Start Time array compare the Hora Inicial < Hora Final and display the End Time Index to be higher.

  • 4

    presents these arrays and an example of the desired result

  • http://php.net/manual/en/function.array-diff.php

  • Hora_ini | Hora_fim<br> 0110000 | 0080000<br> 1130000 | 1140000<br> As you can see on line 1 has a value that is higher at Start Time, I need to know how to check line by line and display an error message. In case the line 1 has an error, because the Initial time is greater than the Final Hora_time

  • That’s not a array, you are reading this line and converting?

  • Here I formatted. I have the two variables that are with the array values. $dtINI ; $dtFIM in them so stored the values.

  • 1

    You can put Start Time and End Time as an example of data in your question?

Show 1 more comment

1 answer

0

If I understand correctly you have two arrays with the same amount of elements, which must be compared on the basis of their index, to determine whether the former has a value greater than the latter.

Let us exemplify:

// Array com as horas iniciais
$arrayHoraInicial = array(
    '09:32:00',
    '11:20:10',
    '13:06:55',
    '19:10:10',
);

// Array com as horas finais
$arrayHoraFinal = array(
    '09:50:00',
    '12:10:00',
    '12:25:15',
    '21:11:08',
);

// Captura quantos elementos existem no array.    
$quantidade = count($arrayHoraInicial); 

// Realiza um loop for para percorrer os arrays.    
for ($i = 0; $i < $quantidade; $i++) {
    // Compara horas string
    if($arrayHoraFinal[$i] < $arrayHoraInicial[$i]) {
        echo "O índice $i apresenta um valor final maior que o inicial";
    }
}

In the above example the printed result will be:

Index 2 shows a final value greater than the initial

Browser other questions tagged

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