I have an array of all options that will be in a select box:
Array
(
[2836] => 4 16:40:00
[2835] => 3 13:20:00
)
There is a second array of options that have a particular flag in the DB:
Array
(
[2835] => 3 13:20:00
)
How can I compare these two arrays to generate a select list that applies a particular class to the matches found in the arrays? I'd love some help thanks!
The view for my select list which receives the first array:
<select>
<?php foreach ($courses as $key=>$course): ?>
<option id="<?php echo $key;?>">
<?php echo $course; ?>
</option>
<?php endforeach;?>
</select>
You can use array_intersect to find those values common for both arrays. Then in your foreach loop you can check if the current key (or value) is from matches array.
$matches = array_intersect($arr1, $arr2);
In the view file:
<select>
<?php foreach ($courses as $key=>$course): ?>
<option id="<?php echo $key;?>" class="<?php echo isset($matches[$key]) ? 'match' : '' ?>">
<?php echo $course; ?>
</option>
<?php endforeach;?>
</select>