unixmiah unixmiah - 4 months ago 11
PHP Question

php in_array search for php array value isn't functioning

For some strange reason on

if(in_array("January", $season)){


when I specify "January" in in_array it matches the array value. But when I use
$search_string
which contains the word January it doesn't match. Any reason why this happens? I removed space before and after the word and it still doesn't match.

$season = array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");

print_r($lines);
Array ( [0] => January - March, November
[1] => 2 travelers: Adult: $9,499 Child: $7,125
[2] => 4 travelers: Adult: $8,315 Child: $6,235
[3] => 6 travelers: Adult: $7,975 Child: $5,980
[4] => June - October, December
[5] => 2 travelers: Adult: $10,799 Child: $8,099
[6] => 4 travelers: Adult: $9,835 Child: $7,375
[7] => 6 travelers: Adult: $9,535 Child: $7,150
)

var_dump($lines);
array(8) { [0]=> string(47) "January - March, November
" [1]=> string(48) "2 travelers: Adult: $9,499 Child: $7,125
" [2]=> string(49) "4 travelers: Adult: $8,315 Child: $6,235
" [3]=> string(47) "6 travelers: Adult: $7,975 Child: $5,980
" [4]=> string(46) "June - October, December
" [5]=> string(49) "2 travelers: Adult: $10,799 Child: $8,099
" [6]=> string(49) "4 travelers: Adult: $9,835 Child: $7,375
" [7]=> string(47) "6 travelers: Adult: $9,535 Child: $7,150
"
}

foreach($lines as $key => $value){
$search_string = explode("-", $value);
$search_string = $search_string[0];
echo "--"; echo $search_string = trim($search_string); echo "--"; // --January--

if(in_array("January", $season)){
echo "Match found";
} else {
echo "Match not found";
}
}


Here is what I got so far:
enter image description here

enter image description here

@spirit, here is where the array line is being created

$lines = array();
foreach(preg_split("/((\r?\n)|(\r\n?))/", $seasonal_price) as $line){
// Only add to array for lines with content
if(trim($line)!="") $lines[] = $line;
}

Answer

try the following:

$lines = preg_split("/[\r\n]+/", strip_tags($seasonal_price), -1, PREG_SPLIT_NO_EMPTY);

foreach ($lines as $key => $value) {
    $search_string = explode("-", $value);
    $search_string = trim($search_string[0]);
    //echo "--"; echo $search_string = trim($search_string); echo "--"; //  --January-- 

    if (in_array($search_string, $season)) {
        echo "Match found";
    } else {
        echo "Match not found";
    }
    echo "\r\n";
}

outputs:

Match found
Match not found
Match not found
Match not found
Match found
Match not found
Match not found
Match not found

just like it sould be, according to your input $lines.


UPDATE: as we found in the chat, data that fills $seasonal_price comes from the database and it contained:

<strong>January - March, November</strong><br/> 

2 travelers:  Adult: $9,499 Child: $7,125<br/> 

4 travelers:  Adult: $8,315 Child: $6,235<br/> 

6 travelers:  Adult: $7,975 Child: $5,980<br/> 

<strong>June - October, December</strong><br/> 

2 travelers:  Adult: $10,799 Child: $8,099<br/> 

4 travelers:  Adult: $9,835 Child: $7,375<br/> 

6 travelers:  Adult: $9,535 Child: $7,150<br/>

so I've added a strip_tags() call to my answer

Comments