bob_the _builder Posted September 16, 2012 Share Posted September 16, 2012 In the following code, where would you use trim to clean whitespaces from fields within the csv? $handle = fopen("products.csv", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $array[] = $data; } Thanks Quote Link to comment https://forums.phpfreaks.com/topic/268448-trim/ Share on other sites More sharing options...
Mahngiel Posted September 16, 2012 Share Posted September 16, 2012 once it's been retrieved and you intend to process it further. Quote Link to comment https://forums.phpfreaks.com/topic/268448-trim/#findComment-1378476 Share on other sites More sharing options...
PFMaBiSmAd Posted September 16, 2012 Share Posted September 16, 2012 You can use array_map to apply the trim function to all elements of your $data array. Quote Link to comment https://forums.phpfreaks.com/topic/268448-trim/#findComment-1378480 Share on other sites More sharing options...
bob_the _builder Posted September 16, 2012 Author Share Posted September 16, 2012 Yea the next step where the array data is used is below.. But I cant be sure where to use trim on $array data foreach($array as $eacharray) { /* Check if product code in each line is in the array of $productcodesarray pulled from the database */ if(in_array($eacharray[0],$productcodesarray)) { /* The product code was found, update the record */ $code = trim($eacharray[0]); $description = trim($eacharray[1]); Quote Link to comment https://forums.phpfreaks.com/topic/268448-trim/#findComment-1378481 Share on other sites More sharing options...
Barand Posted September 16, 2012 Share Posted September 16, 2012 $handle = fopen("products.csv", "r"); while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { $array[] = array_map('trim', $data); } Quote Link to comment https://forums.phpfreaks.com/topic/268448-trim/#findComment-1378483 Share on other sites More sharing options...
bob_the _builder Posted September 17, 2012 Author Share Posted September 17, 2012 Thanks.. array_map did the job.. Quote Link to comment https://forums.phpfreaks.com/topic/268448-trim/#findComment-1378501 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.