M
M
Matthew Stafford2019-10-17 16:36:07
PHP
Matthew Stafford, 2019-10-17 16:36:07

Why does in_array only see the first element in the loop?

There is an array that stores the time in the HH:MM format. Array ( [0] => 16:30 [1] => 17:00 )
If you use the Then function in the loop, it sees the first element and returns TRUE , but if you try to check the second element of the array, it does not return anything. Why? And how to fix it?
in_array('16:30', $array);

in_array('17:00', $array);

Answer the question

In order to leave comments, you need to log in

3 answer(s)
B
BoShurik, 2019-10-17
@matthewstafford

The problem is that the string from which the array was created at the end contains (most likely) a newline, so the second (aka last element) contains non-printable characters that interfere with the comparison. will solve the problem

D
Dmitry, 2019-10-17
@Compolomus

Add third argument to function
https://www.php.net/manual/ru/function.in-array.php
Third argument true

A
Andrey, 2019-10-17
@VladimirAndreev

and $array is definitely not overwritten anywhere in the loop and not modified in any way?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question