dongtan8532 2019-01-09 13:09 采纳率: 0%
浏览 26
已采纳

从PHP数组创建XLS文件(行和列干扰)

First of all I use this class for creating excel files via XML: https://github.com/oliverschwarz/php-excel

My headers that I want to export to Excel;

$headers = array("A","B","C");

My arrays that I want to export to Excel;

$example = array("a1","a2","a3");
$example2 = array("b1","b2","b3");
$example3 = array("c1","c2","c3");

Then I merge my arrays in 1 array with these codes;

$result = array($headers,$example,$example2,$example3);

In conclusion I start the action;

$xls = new Excel_XML('UTF-8', false, 'Test');
$xls->addArray($result);
$xls->generateXML('xls-file');

After that, these class creates a XLS file as expected but; that XLS file scheme is unexpected because of my arrays's types. So I need help my friends, what should I do? Thanks...

Output is like that;

A_|__B__|_C
A1|__A2_|_A3
B1|__B2_|_B3
C1|__C2_|_C3

Output I desire is like that;

A_|__B__|_C
A1|__B1_|_C1
A2|__B2_|_C2
A3|__B3_|_C3
  • 写回答

2条回答 默认 最新

  • doushichun9409 2019-01-09 13:19
    关注

    You can use array_map() to re-organise the data from your results and then use array_unshift() to push in the headers to the start of the array...

    $result = array_map(null, $example, $example2, $example3);
    array_unshift($result, $headers);
    print_r($result);
    

    which will give.

    Array
    (
        [0] => Array
            (
                [0] => A
                [1] => B
                [2] => C
            )
    
        [1] => Array
            (
                [0] => a1
                [1] => b1
                [2] => c1
            )
    
        [2] => Array
            (
                [0] => a2
                [1] => b2
                [2] => c2
            )
    
        [3] => Array
            (
                [0] => a3
                [1] => b3
                [2] => c3
            )
    
    )
    

    If your data is created as an array of arrays, then the following does the same sort of thing, passing the array in using ... to pass each element in as a new parameter...

    $example = [ ["a1","a2","a3"], ["b1","b2","b3"], ["c1","c2","c3"]];
    $result = array_map(null, ...$example);
    array_unshift($result, $headers);
    print_r($result);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害