开发者

How do you handle associative arrays in CSV imports?

开发者 https://www.devze.com 2023-02-03 06:21 出处:网络
I\'m building a CSV import where the first row is the column names of everything else.Instead of having $row[0] I\'d like to use $row[\'id\'] to keep the code readable and make it easier for future de

I'm building a CSV import where the first row is the column names of everything else. Instead of having $row[0] I'd like to use $row['id'] to keep the code readable and make it easier for future developers. The problem is... I can't think of an efficient way to do this...

while (($current = fgetcsv($handle, 1000, "\t")) !== false) {
    if ($row == 0) {

    }
}

The only methods I can think of require a bit of legwork and I'd like t开发者_如何学Pythono just have a simple solution that's clean and easy (every developers dream, I know). So I thought I'd post here and see if someone had a better method than what I'm thinking...


Nevermind... just found array_combine on PHP.net... it looks like exactly what I need

 <?php
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);

print_r($c);
?>

Array
(
    [green]  => avocado
    [red]    => apple
    [yellow] => banana
)

I'd delete my question but I think this would be useful for some people to know...

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号