开发者

Error with explode and in_array in php

开发者 https://www.devze.com 2023-03-26 16:52 出处:网络
I would like to check the following condition with php $string = \'10-15~15-20~20-25~\'; $stringArray = explode(\'~\',rtrim($string,\'~\'));

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

精彩评论

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

关注公众号