2

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?

6
  • 4
    Don't just use echo - use 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_dump can help you fix all your code problems :-) Commented Aug 30, 2013 at 22:26
  • 3
    @Itay quotes make no difference — array_search is non-strict by default. (OK, technically not true, if the array contained string("5 ") and $roomNb was int(5), then this would make a difference…) Commented Aug 30, 2013 at 22:28
  • I just replicated the scenario in my terminal and it worked fine, maybe try casting (int) when you're storing the values in the array? Commented Aug 30, 2013 at 22:32
  • @cbuckley - I think even in that case it's OK. $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). Commented Aug 30, 2013 at 22:43
  • @ChicagoRedSox yes, my point was that the quotes would make a difference in that scenario, as array_search('5', ['5 ']) would return false! There was a deleted comment that said removing the quotes around $roomNb would fix it. Commented Aug 31, 2013 at 10:20

2 Answers 2

1

Most likely $key is false which when sent to echo will appear as nothing. Instead you should use var_dump to determine the value of $key

False is returned from array_search when the needle is not found

Null is only returned when the parameters you pass to array_search are invalid

http://php.net/manual/en/function.array-search.php

Test this code here using http://writecodeonline.com/php/:

echo false;
echo null;
var_dump(false);
var_dump(null);

$code = array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);
$roomNb = 5;

var_dump(array_search("$roomNb", $code));

Should work just fine. echo will output nothing. var_dump will output stuff. And the key is found at 4.

Sign up to request clarification or add additional context in comments.

Comments

0

Assuming that your array will always contain contiguous numbers, and you can verify that your second SQL call returns one of those numbers, I would say something like

$value = $freeRooms[$roomNb] 

I'm personally unfamiliar with how array_search executes, however if you wanted to write your own, I assume it would look something like this

function IndexOf($searchValue,$array)
{
    $i = 0;
    for( $i = 0; $i < count($array); $i++ ) 
        if( $array[$i] == $searchValue )
            return $i;
}

1 Comment

The array doesn't contain contiguous numbers: it starts with 1, not 0, and it skips 13.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.