Jump to content

Problem with changing Array key


guyfromfl

Recommended Posts

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:

  Quote

Array ( [0] => Array ( [FULL NAME] => Some Person [PHONE] => 1234567890 [ALT. PHONE] => => you@me.com [ADDRESS] => 123 Birch Drive [CITY] => Anytown [sTATE] => FQ [ZIP] => 98765 [COUNTRY] => US ) [First Name] =>

 

 

Any help would be much appreciated.  Thanks

 

Link to comment
https://forums.phpfreaks.com/topic/244262-problem-with-changing-array-key/
Share on other sites

$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;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.