Jump to content

Yet another Multidimensional Associative Array problem...


guyfromfl

Recommended Posts

I know this is right in front of me, but I need to add an element to each row of an array..

 

I have this array:

Array ( [0] => Array ( [Elements] => values )
[1] => Array ( [Elements] => values ) )

 

Now, I want to add an element to the end of each one that holds the file name of the originating data.

 

The part of code this takes place in, is in a class method to look for duplicates already in the database.  If it is a duplicate, we add the $fileData iteration to the $duplicates array which gets returned to the calling function. It basically looks like this:

 

Calling Code:

<?php
while($data = fgetcsv($handle)) {			
	$leadDataLine = array_combine($headers, $data);

	// Some data formatting on $leadDataLine not important for this question...

	// Add the line to the stack
    		$leadData[] = $leadDataLine;
	//array_push($leadData, $leadDataLine);
	unset($leadDataLine);		

}

$dup[] = $lead->process($leadData);
?>

 

The lead class:

<?php

public function process(&$fileData) {
	$duplicates = array();
	// Process the information

	foreach($fileData as $row) {
			// If not a duplicate add to the database
			if (!$this->isDuplicate($row)) {
				// Add the lead to the database.
				$this->add($row); 
			} else {
				// is a duplicate, add to $dup

				$duplicates[] = array("Elements" => $row['Values']);
									   

                                        /* 
                                         * Here is where I want to add the file name to the end of $duplicates
                                         * This has to be here because this class handles different sources of data,
                                         * Not all data will have a FileName key
                                         */
				if (array_key_exists("FileName", $row))
					$duplicates["FileName"] =  $row["FileName"];
					// array_push($duplicates, $row["FileName"]);

			}

	}
	print_r($duplicates);
	return $duplicates;

}
?>

 

 

What happens with the code I have, or using array_push:

 


Array ( [0] => Array ( [Elements] => values) [FileName] => correct_file.csv [1] => Array ( [Elements] => Values) ) 

 

Notice, it is not on element 1..

 

What am I doing wrong here.

Link to comment
Share on other sites

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.