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()?
jointhe remaining columns in that row, too.