开发者

Error with explode and in_array in php

I would like to check the following condition with php

$string = '10-15~15-20~20-25~';

    $stringArray = explode('~',rtrim($string,'~'));

    if (in_array('20-25', $string开发者_JAVA技巧Array)) {
       echo 'Found';
    }
    else
    {
        echo 'Not found';
    }

20-25 is present in my array but, it always shows not found


There are some errors in your code. Here is a corrected version.

$string = '10-15~15-20~20-25~';
$stringArray = explode('~',rtrim($string,'~')); // corrected here, missing "$" before "string"
if (in_array('20-25', $stringArray)) { // corrected here, wrong variable name "priceArray"
   echo 'Found';
}
else
{
    echo 'Not found';
}


replace $priceArray with $stringArray. It's just a typo. You are searching "20-25" in non-initialized variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜