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.
精彩评论