Я знаю, что это прямо передо мной, но мне нужно добавить элемент в каждую строку массива ..
У меня есть этот массив:
Array (
[0] => Array (
[Elements] => values
)
[1] => Array (
[Elements] => values
)
)
Теперь я хочу добавить в конец каждого элемента, который содержит имя файла исходных данных.
Часть кода, в которой это происходит, находится в методе класса для поиска дубликатов, уже находящихся в базе данных. Если это дубликат, мы добавляем итерацию $ fileData в массив $ duplicates, который возвращается в вызывающую функцию. В основном это выглядит так:
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);
Ведущий класс:
<?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;
}
Что происходит с кодом, который у меня есть, или с использованием array_push:
Array (
[0] => Array (
[Elements] => values
)
[FileName] => correct_file.csv
[1] => Array (
[Elements] => Values
)
)
Обратите внимание, это не элемент 1 ..
Что я здесь не так делаю.