3

I have a CSV file as follows:

first-key,first-value
second-key,second-value
third-key,third-value
fourth-key,fourth-value

I am reading these in to an array using:

$tmp_array = array_map('str_getcsv', file('./values.csv'));

However, this results in this array:

Array
(
    [0] => Array
        (
            [0] => first-key
            [1] => first-value
        )

    [1] => Array
        (
            [0] => second-key
            [1] => second-value
        )

    [2] => Array
        (
            [0] => third-key
            [1] => third-value
        )

    [3] => Array
        (
            [0] => fourth-key
            [1] => fourth-value
        )

)

What I would like is this array:

Array
(
    [first-key] => first-value
    [second-key] => second-value
    [third-key] => third-value
    [fourth-key] => fourth-value
)

One way I can achieve this is by doing this:

$tmp_array = array_map('str_getcsv', file('./values.csv'));
$array = [];
foreach ($tmp_array as $row) {
    $array[$row[0]] = $row[1];
}

Is there a better way of doing this? Perhaps using array_map()?

1
  • I think it is more common to see CSV files where the top row is optionally headers, and each subsequent row are isolated field/columns. Your CSV isn't invalid but I think most examples out there assume otherwise. Because of that, think your second way of doing it is probably best. Are you absolutely certain that your second column will never contain a comma? If it ever could, you'd want to join the remaining columns in that row, too. Commented Jul 28, 2020 at 13:26

1 Answer 1

2

It depends on your definition of better in this context. I think your solution is good enough. Is better:

  • not using foreach?
  • less lines?
  • less variables?
  • using more standard library functions?

I want to post 2 propositions:

// 1
$tmp = array_map('str_getcsv', file('data.csv'));
$data = array_combine(array_column($tmp, 0), array_column($tmp, 1));

var_dump($data);

// 2
$data = [];
array_walk(
  $tmp,
  function($e) use (&$data) {$data[$e[0]] = $e[1];}
);

var_dump($data);

Output:

array(4) {
  ["first-key"]=>
  string(11) "first-value"
  ["second-key"]=>
  string(12) "second-value"
  ["third-key"]=>
  string(11) "third-value"
  ["fourth-key"]=>
  string(12) "fourth-value"
}
array(4) {
  ["first-key"]=>
  string(11) "first-value"
  ["second-key"]=>
  string(12) "second-value"
  ["third-key"]=>
  string(11) "third-value"
  ["fourth-key"]=>
  string(12) "fourth-value"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! In regards to "better", I should have elaborated. I was hoping there was a one liner that doesn't involve using two different arrays (i.e. so CSV the file could be read directly in to an array).

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.