I have a regular array that contains numbers from 1 to 14 that is generated by retrieving INTs from a SQL result column.
Using another query I'm returning another INT.
I'm searching for this INT in my array.
$key = array_search("$roomNb", $freeRooms);
However when I try to echo this key nothing appears. It is empty.
If I echo $freeRooms using a loop I get:
1
2
3
4
5
6
7
8
9
10
11
12
14
If I echo $roomNb I get
5
So I can't understand why I'm not getting anything in return. I should be expecting a key of 4 no? What could be causing this?
var_dump($roomNb, $freeRooms). I'm willing to bet there's an extra space or other character in your needle or haystack. In general,var_dumpcan help you fix all your code problems :-)string("5 ")and $roomNb wasint(5), then this would make a difference…)$a = ['4', '5 ', '6']; echo array_search(5, $a);that echoes 1, since PHP's string->int conversion looks for the first non-whitespace character and then checks if that is the start of a sequence of digits before the next non-digit character matching the search input. It would even work if the value in the array were '5g' or ' 5 6' (space between the 5 and 6).array_search('5', ['5 '])would return false! There was a deleted comment that said removing the quotes around$roomNbwould fix it.