guyfromfl Posted August 8, 2011 Share Posted August 8, 2011 I'm suffering from Monday impaired thinking... need a little help.. Basically, I am taking an array of CSV files, and going through each of them. The problem is the code I have written is designed around different header names...For example, the old way has First Name and Last Name fields. The new on put them together into one field called Full Name. In order to use my old code, I just need to take Full Name and explode the values at the first space " ". For some reason this doesn't work and I cant figure it out. It will push First Name on the end of the first element, with no value... All the echos are to help try to debug.. foreach($fileList as $f) { if (($handle = fopen(UPLOAD_PATH.$f, "r")) != FALSE) { $headers = fgetcsv($handle); echo $handle; echo count($headers); // Read the rest of the file while($data = fgetcsv($handle)) { echo strrpos($data['FULL NAME'], " ") . "<br />"; $leadData[] = array_combine($headers, $data); $leadData['First Name'] = substr($leadData['FULL NAME'], 0, strrpos($leadData['FULL NAME'], " ")); } echo $leadData['First Name'] . "<br />"; print_r($leadData); } } Output: Array ( [0] => Array ( [FULL NAME] => Some Person [PHONE] => 1234567890 [ALT. PHONE] => => [email protected] [ADDRESS] => 123 Birch Drive [CITY] => Anytown [sTATE] => FQ [ZIP] => 98765 [COUNTRY] => US ) [First Name] => Any help would be much appreciated. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/244262-problem-with-changing-array-key/ Share on other sites More sharing options...
requinix Posted August 8, 2011 Share Posted August 8, 2011 $leadData[] = array_combine($headers, $data); $leadData['First Name'] = substr($leadData['FULL NAME'], 0, strrpos($leadData['FULL NAME'], " ")); When you array_combine() the headers and data, store the array someplace else so that you can work on it. Once you've split the name into first and last you can add it to the final $leadData. while($data = fgetcsv($handle)) { $leadDataLine = array_combine($headers, $data); $fn = $leadDataLine["FULL NAME"]; $leadDataLine["First Name"] = strtok($fn, " "); // grab everything up to the first space $leadDataLine["Last Name"] = strtok(""); // grab everything else $leadData[] = $leadDataLine; } Quote Link to comment https://forums.phpfreaks.com/topic/244262-problem-with-changing-array-key/#findComment-1254537 Share on other sites More sharing options...
guyfromfl Posted August 8, 2011 Author Share Posted August 8, 2011 That did it. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/244262-problem-with-changing-array-key/#findComment-1254540 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.