Q:

array_key_exists in a foreach

Hi There I am running this code currently, 
<?php foreach($search_results as $rslt) : ?>
    <?
    $code = $rslt['code'];
    if(array_key_exists($code, $short_list)) {
        $set = "set";
    }
    ?>
    <div class="row <? echo $set;?>"></div>

What I am trying to achieve is that if the array equals the $rslt['code'] then give the div the class of set otherwise don't the array I checking against looks like this, 
    Array
(
    [849650047] => Y
    [849652539] => Y
    [849652774] => Y
    [849656557] => Y
    [849652014] => Y
)

However every time I loop all my divs get set with the 'set' class? It should only be where the array_key equals the current $code

A:

Well, they are all set, because you're never initializing the $set variable:
<?php foreach($search_results as $rslt) : ?>
    <?
    $set = '';
    $code = $rslt['code'];
    if(array_key_exists($code, $short_list)) {
        $set = "set";
    }
    ?>
    <div class="row <? echo $set;?>"></div>

Also, just use isset() instead of array_key_exists (it's more efficient, and less wordy):
    if(isset($short_list[$code])) {
        $set = "set";
    }

