Jump to content

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:

 

 

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.