简体   繁体   中英

How can i Create a utility function for check null value coming from CSV and replace to print NULL

This is the function I have used to print NULL but I want to check all values in one function and replace where there is no value than print NULL in the database and I am using Codeigniter.

$candidate = $this->csvreader->parse_file($_FILES['file']['tmp_name']);
foreach($candidate as $row){
                        if(empty($row['EMAIL_ADDRESS']))
                        {
                            $email = NULL;
                        }
                        else
                        {
                            $email = $row['EMAIL_ADDRESS'];
                        }
                        if(empty($row['CONTACT_NO']))
                        {
                            $contact_no = NULL;
                        }
                        else
                        {
                            $contact_no = $row['CONTACT_NO'];
                        }
                        if(empty($row['RECEIVE_DATE']))
                        {
                            $receive_date = NULL;
                        }
                        else
                        {
                            $receive_date = $row['RECEIVE_DATE'];
                        }
                        if(empty($row['SELENIUM']))
                        {
                            $selenium = NULL;
                        }
                        else
                        {
                            $selenium = $row['SELENIUM'];
                        }
}

My Array Looks Like This

Array
(
 [CANDIDATE_CODE] => XYZ
 [CANDIDATE_NAME] => XYZ
 [CONTACT_NO] => 
 [EMAIL_ADDRESS] => 
 [SOURCE] => XYZ
 [RECEIVE_DATE] => 
 [CURRENT_STATUS] => XYZ
 [INTERVIEW_DATE] => 2019-05-04
 [JAVA_PROGRAMMING] => 
 [COMMUNICATION] => 
 [CONCEPTUAL_KNOWLEDGE] => 
 [COMMENT_1] => abc
 [STATUS_1ST_ROUND] => 
 [TEST_DATE] => 
 [TEST_PLATFORM] => 
 [TEST_SCORE] => 
 [COMMENT_2] => 
 [STATUS_2ND_ROUND] => 
 [3RD_ROUND_DATE] => 
 [COMMENT_3] => 
 [STATUS_3RD_ROUND] => 
 [DATE] => 
 [COMMENT_4] => 
 [FINAL_DATE] => 
 [COMMENT_5] => 
)

in the array there is many null value so i have to put NULL in the database how can i do this in function call

The solution is to have the database use NULL as the default field values. Then if not data is provided during insert/update then, when the row is retrieved, the values returned will be NULL and you won't need all those checks.

$data2 = array('CANDIDATE_CODE' => 'XYZ','CONTACT_NO' => '');

$data2 = array_map(function($value_) { return ($value_ == null) ? 'null' : trim($value_); }, $data2); print_r($data2);

Array ( [CANDIDATE_CODE] => XYZ [CONTACT_NO] => null )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM