【问题标题】:How to insert associative array into csv using thephpleague如何使用 phpleague 将关联数组插入 csv
【发布时间】:2017-06-24 23:19:18
【问题描述】:

我已经在laravel5.1中使用了phpleague库 http://csv.thephpleague.com/inserting/

我想要一个将关联数组添加到 CSV 中的示例。 现在我已经使用了这个功能 insertOne($array) 用于 CSV 的标头和 insertAll($array) 为其列值。

我的代码是 -

    $path = public_path().'/csv/'.$filename;
    $csv = Writer::createFromPath(new \SplFileObject($path, 'w+'),   'a+');
    $csv->insertOne($headers);
    $csv->insertAll($data);
    $csv->output($path);

我的 $data 数组有一个关联数组(数组内的数组),但是如果 $data 有一个关联数组,insertAll() 方法会给我一个错误。

请告诉我如何处理联想 arrya?

【问题讨论】:

    标签: php arrays csv laravel-5 thephpleague


    【解决方案1】:

    像这样将关联数组转换为数字索引数组

    $csv->insertAll(array_map('array_values', $data));
    

    【讨论】:

    • 如何确保它们符合 CSV 列的预期顺序?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-29
    • 1970-01-01
    • 2017-06-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多