-5

I have the following csv

CSV formate

And i want to convert it like :

$data = array(
 'firstName[0]' => 'test1',
 'lastName[0]' => 'test1',
 'email[0]' => '[email protected]'
 'firstName[1]' => 'test2',
 'lastName[1]' => 'test2',
 'email[1]' => '[email protected]'
 ...
 ...
);

The following code gives me a nested array and each row have an index in that array,

$csvData = file_get_contents('filename.csv');
$lines = explode(PHP_EOL, $csvData);
$array = array();
foreach ($lines as $key => $value) {
    $array[$key] = str_getcsv($value);
}

Output :

Output

which is in my case wrong, How will i be able to achieve that ?

10
  • Iterate your array and build a new one. Commented Mar 18, 2019 at 12:39
  • For closers - OP needs format of array which is distinct form the one, get by str_getcsv and similar. Commented Mar 18, 2019 at 12:41
  • 1
    I think your understanding of array structure is wrong. To get the data from the array that you want, you'd have to use $data['firstName[0]']. To get the data as $data['firstName'][0], then you'd need to have it as $data = array( 'firstName' => array( 0=>'test1'));. Commented Mar 18, 2019 at 12:45
  • @u_mulder Basically the OP need to write some code rather than hope that mashing a few built in functions can do it all for them Commented Mar 18, 2019 at 12:45
  • You should have a look at the Collection class in Laravel, which can help you transform your data Commented Mar 18, 2019 at 12:46

2 Answers 2

2

I hope it will help you

public function importCsv()
{
    $file = public_path('upload/test.csv');

    $customerArr = $this->csvToArray($file);

    for ($i = 0; $i < count($customerArr); $i ++)
    {
    //   $customerArr[$i];
       //get data and insert or you ca use
    }

     print_r($customerArr);

}


function csvToArray($filename = '', $delimiter = ',')
{
    if (!file_exists($filename) || !is_readable($filename))
        return false;

    $header = null;
    $data = array();
    if (($handle = fopen($filename, 'r')) !== false)
    {
        while (($row = fgetcsv($handle, 1000, $delimiter)) !== false)
        {
            if (!$header)
                $header = $row;
            else
                $data[] = array_combine($header, $row);
        }
        fclose($handle);
    }

    return $data;
}
Sign up to request clarification or add additional context in comments.

Comments

0
<?php
$csv = array_map('str_getcsv', file('filename.csv'));

for ($j = 1; $j < count($csv); $j++) {
    for ($i = 0; $i < count($csv[0]); $i++) {
        $myArray[$csv[0][$i].$j] = $csv[$j][$i];
    }
}

print_r($myArray);

Not the most elegant solution but will work for you. I used for loops so i can use their counters easier. The output result is :

Array
(
    [firstName1] => test1
    [lastName1] => Test1
    [email1] => [email protected]
    [firstName2] => test2
    [lastName2] => Test2
    [email2] => [email protected]
)

And my csv file looked like this:

firstName,lastName,email
test1,Test1,[email protected]
test2,Test2,[email protected]

The first loop starts the counter from position 1 so i avoid the headers, and will loop around the main csv array.

The nested loop will go through each of the nested arrays and based on the 1st one that holds the header info will concatenate the new key fields and create your new array.

If you want to have it exactly like in your example, with the brackets and the counter to start from 0 for the key field then you can simple:

<?php
$csv = array_map('str_getcsv', file('filename.csv'));

for ($j = 1; $j < count($csv); $j++) {
    for ($i = 0; $i < count($csv[0]); $i++) {
        $counter = $j-1;
        $myArray[$csv[0][$i].'['.$counter.']'] = $csv[$j][$i];
    }
}

print_r($myArray);

Which will output for you an array just like you requested in your question:

Array
(
    [firstName[0]] => test1
    [lastName[0]] => Test1
    [email[0]] => [email protected]
    [firstName[1]] => test2
    [lastName[1]] => Test2
    [email[1]] => [email protected]
)

Comments

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.