array_search()没有找到0个元素 [英] array_search() not finding 0th element

查看:147
本文介绍了array_search()没有找到0个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有股票行情符号的大阵列,因为我是为一个项目编写模拟股市的web应用。我的一个辅助功能是一家以确定输入的股票是否是通过检查符号数组有效。要,我使用 array_search做到这一点()
我的问题是一所经检查边缘情况。看来,搜索不拿起第0个元素,即使它拿起其他元素完全没问题。
这里是阵列的一部分:

I have a large array of stock ticker symbols, as I am for a project writing a stock market simulation webapp. One of my helper functions is one to determine whether the entered stock is valid by checking the array for the symbol. To do this, I am using array_search(). The problem I have is one by checking edge cases. It seems that the search isn't picking up the 0th element, even though it picks up other elements completely fine. Here is part of the array:

[0] => AAC
[1] => AACC
[2] => AACOU
[3] => AACOW
[4] => AAIT
[5] => AAME
[6] => AAON
[7] => AAPL
[8] => AAWW
[9] => AAXJ
[10] => ABAX
[11] => ABCB
[12] => ABCD
[13] => ABCO
[14] => ABFS
[15] => ABIO
[16] => ABMD
[17] => ABTL
[18] => ABVA
[19] => ACAD
[20] => ACAS
[21] => ACAT
[22] => ACCL

等等,等等。
正如前面所说的,它工作正常进行的其他元素,只是没有第0之一。它返回 FALSE 为AAC搜索时。

下面是PHP code我使用。

Here is the PHP code I am using.

<?php   
                if(isset($_GET[stock])) {
                    $ticker = $_GET[stock];
                    $ticker = trim($ticker);
                    print("<pre>Ticker is $ticker</pre>");

                    print("Validity: " .        Stock::isValidStock($ticker));

                    print('<pre>');
                    $stock = Stock::getStockList();
                    print_r($stock);
                    print((bool)array_search('AAC', $stock));
                    print('</pre></br>');
                }
            ?>

下面是测试网页,我使用。
您可以通过编辑URL查询字符串改变的股票。
正如我所说的,?股票= AAC 返回false,而像?股票= GOOG 是真实的。
感谢您的帮助!

Here is the test web page I'm using. You can change the stock by editing the query string in the url. As I said, ?stock=AAC returns false, while something like ?stock=GOOG is true. Thanks for any help!

推荐答案

array_search ()如果发现返回第一场比赛的关键。当所说的关键是0,你施放它变成一个布尔值,它会变成假的。

array_search() returns the key of the first match if one is found. When said key is 0 and you cast it into a bool, it will become false.

您想要做的是把它比作使用的不相同的运算符 ===

What you want to do is compare it to false using the not identical operator === :

//if you don't need the index you can skip assigning it to a variable
$index = array_search('AAC', $stock); 

if ($index !== false) 
{
    // found!
}

这篇关于array_search()没有找到0个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆