简体   繁体   中英

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', $stringArray)) {
       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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM