When I use values fetched from a comma-separated list in a db to form an array, I am having trouble getting them to behave the same way as hard coded arrays.
I believe this is due to the syntax within the array when it's created from db values, specifically that values aren't being encased in quotes, but I haven't successfully figured out how to insert them into the array itself.
For example, when I run the snippet which I've posted at the bottom of the page using:
Array (hard coded):
$colors_loveArray = array('Black','Charcoal','Light_Gray','White','Royal_Blue','Dodger_Blue','Red');
The output is:
Array = 'Black','Charcoal','Light_Gray','White','Royal_Blue','Dodger_Blue','Red',
Matches: Black
Array formed with values fetched from db (comma-separated list):
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach ($rows as $row) {
$colors_loveArray[] = $row['colors_love'];
}
The output is:
Array = 'Black,Charcoal,Light_Gray,White,Royal_Blue,Dodger_Blue,Red',
No Match Black
Code which is being run:
$Color='Black';
echo "$colors_loveArray = ";
foreach ($colors_loveArray as $value) {
echo "'".$value."',";
}
if (in_array($Color, $colors_loveArray)) {
echo "Matches: ", $Color;
}
Else {
echo "No Match ", $Color;
}