0

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.

2 Answers 2

1

If you do

$x[] = 'yo'

you're pushign a value onto the top level of the array. If you want push a new item onto a sub-element of this array, you have to explicitly state which sub-element:

$x[0][] = 'yo';
Sign up to request clarification or add additional context in comments.

1 Comment

Man I'm out of it! ha.. I tried $x[]["FileName"].. just had to add a variable to keep track of the iteration and works fine...Thanks
0

indexes are unique. based on your code it looks like you are constantly overwriting $duplicates['filename'] so that only the last file name is stored.

1 Comment

I changed it to $duplicates[$line]["FileName"] = $row["FileName"]; where line counts each time the foreach loop is restarted... That should take care of that problem, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.