Assuming your array is not sorted (i.e. must use linear search), this is how you should do it:
function assignstate($state_id, $slist) {
$size = count($slist);
for ($i = 0; $i < $size; $i++) {
if ($slist[$i]['state_id'] == $state_id) {
return $slist[$i]['statename'];
}
}
return false;
}
Count the array size.
Scan from the first element of the array to the last
If the state_id of current element matches that provided, then return statename of current element.
If no state_id matches, then return false.
Clean and simple.
To actually use it, you would validate $_SESSION['custvars']['state_id'] (perhaps assigning it to $cust_state_id), and if all is okay call:
if (($statename = assignstate($cust_state_id, $slist)) !== false)