Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8243773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:37:39+00:00 2026-06-07T21:37:39+00:00

I have a foreach loop to add the values from an array into a

  • 0

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] =>
)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T21:37:42+00:00Added an answer on June 7, 2026 at 9:37 pm

    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:

    foreach($data as $field => &$value)
    {
    
    
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this issue where I am trying to add values from one array
I have a datatable that I add to the chart in foreach loop as
i want to have a foreach loop where the initial array is changed inside
I have this foreach statement that is supposed to cycle through an array, but
I have encountered a problem when trying to add values to an array through
i have two foreach loop to display some data, but i want to use
I have a foreach loop in jQuery going through each 'div.panel', I need to
Right now I have a foreach loop that grabs the first link with an
in a ASP.NET application (MVC) I have a foreach loop that loops through a
I know I'm probably missing something easy, but I have a foreach loop and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.