I have a foreach loop to add the values from an array into a codeigniter where function. When I pass a value with content into this loop, it does not alter. However, when a null value goes in, it comes out with the value of the item before it in the array.
Here is the code:
if(is_null($data['psc_opt_5'])){ echo "null"; }else{ echo $data['psc_opt_5']; }
foreach($data as $field => $value)
{
if(!is_null($value))
{
$this->db->where($field, $value);
}
else
{
$this->db->where($field." IS NULL");
}
}
if(is_null($data['psc_opt_5'])){ echo "null"; }else{ echo $data['psc_opt_5']; }
The array holds psc_opt_1 – psc_opt_5.
Any ideas?
Edit
An the first if statement proves that psc_opt_5 is null before the loop and here are the arrays I’m testing with:
Array
(
[psc_opt_1] => 1
[psc_opt_2] => 2
[psc_opt_3] => 3
[psc_opt_4] => 4
[psc_opt_5] =>
)Array
(
[psc_opt_1] => 5
[psc_opt_2] => 2
[psc_opt_3] => 3
[psc_opt_4] => 4
[psc_opt_5] =>
)Array
(
[psc_opt_1] => 7
[psc_opt_2] => 2
[psc_opt_3] => 3
[psc_opt_4] => 4
[psc_opt_5] =>
)
The problem seems to be that the foreach doesn’t like the null value being passed into it, so it retains the value of the previous cycle. I found that if I add the ampersamp as below, it passes the null value through: