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:
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.