dongxi5423 2013-08-30 22:23
浏览 127
已采纳

PHP:array_search返回null

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?

  • 写回答

2条回答 默认 最新

  • dongqian5384 2013-08-30 23:07
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。