php - How to remove new line or line breaks while parsing data from an array -
i trying parse csv file insert csv contents database. after data parsed array looks like
array ( [airportcode ] => gka )
and extract airportcode gka , insert it. looks fine key , value in above array having line breaks.
sample code:
foreach($data['csvdata'] $field) { print_r($field); $airportcode = str_replace(php_eol, '', $field['airportcode']); echo $airportcode ; }
but airportcode returning empty. want get-rid of line breaks. can me...?
untested
i'm not sure str_replace()
capabilities on recognizing newline characters using escape reference, it's worth shot.
however airport code returning nothing, because actual key seems have \n (new line character) in.
function fixarraykey(&$arr) { $arr=array_combine(array_map(function($str){ return str_replace("\n ","",$str); },array_keys($arr)),array_values($arr)); foreach($arr $key=>$val) { if(is_array($val)) fixarraykey($arr[$key]); } }
for array values use:
function fixarrayvalue($arr) { $return = array(); foreach($arr $key => $value){ $return[$key] = rtrim($value, "\n"); } return $return }
Comments
Post a Comment