Reel
Reel

Reputation: 364

Passing two arguments to the function inside of array_map

I'm working parsing a CSV files, big CSV files and I need to parse a second argument to the str_getcsv function (delimiter), but I honestly have no idea how...

I tried to bypass the array map function, but the php throws an error at me, that allowed memory size was exceeded. I also tried array_walk, but I got the same result.

Here is what the function looks like, right now...

function parseIt($file)
{
    $file = file($file);
    // foreach ($file as $key => $value)
    // {
    //     $rows[] = str_getcsv($value, ';');
    //     unset($file[$key]);
    // }
    // array_walk($file, function(&$item)
    // {
    //     $item = str_getcsv($item, ';');
    // });

    // exit();
    return array_map('str_getcsv', file($file));
}

So, right now, I am trying to figure out how to pass second parameter to str_getcsv through array_map...

Upvotes: 0

Views: 356

Answers (1)

Pyton
Pyton

Reputation: 1319

You can set delimiter in array_map but:

<?php

$string = [
    "this;is;a;string;aaa",
    "this2;is;a;string;aaa",
    "this3;is;a;string;aaa",
    "this4;is;a;string;aaa",
];

$output = array_map('str_getcsv', $string, [";", ";", ";", ";", ]);

in [";"] You must put as many delimiters as rows in input array like in eg.

Other parameters from str_getcsv can be done same way.

Upvotes: 1

Related Questions