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 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入