如何在phpExcel中将数组打印为行和列? [英] How can i print array as row and column in phpExcel?

查看:175
本文介绍了如何在phpExcel中将数组打印为行和列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在浏览器中的数组输出,我需要在PHPEXCEL中将其打印为行和列.请建议我如何将其导出为行和列.

Here is my array output in browser which i need to print as row and column associatively in PHPEXCEL.Please suggest me how to export it as row and columns.

FirstArray
(
    [0] => EY>Yes
    [1] => Media Type>Category B
    [2] => Coverage Type>Quote
    [3] => Industry Programs>Communications
    [4] => Score>1
) 

代码-

while ($i < count($labels)) {

                $j = 0;
                while ($j < count($dd1)) {
                    // for($j=0;$j<count($dd1);$j++) {
                    $temp = explode(">", $dd1[$j]);


                    if ($temp[0] == $labels[$i]) {
                        $name = explode(">", $dd1[$j]);
                       echo '<pre>First';
                        print_r($name);
                    }
                    $j++;
                }

完整代码,我试图将$ name数组打印为行和列-

complete code i am trying to print $name array as row and column -

  $inn_table = "";

for($i=0;$i<count($labels);$i++) {

 for($j=0;$j<count($dd1);$j++) {
                  $temp = explode(">",$dd1[$j]);    

                 if($temp[0]==$labels[$i]) {
                     $name = explode(">",$dd1[$j]);

//here i need to print my $name array data

        }

  }
   echo $inn_table; 

推荐答案

第一步,您可以收集所有列名称一次并创建第一个静态行,该行将作为列使用:

At first step you can collect once all column names and create first static row, which will work as columns:

foreach($labels[0] as $ind=>$label){

        $letter = range('A', 'Z')[$ind];
        $tmp = explode('>',$label); 
        $col_names[] = $tmp[0];
        echo $letter.'1'."\r\n";
        //$objPHPExcel->getActiveSheet()->setCellValue($letter.'1',$tmp[0]); 
        echo "Column -> $tmp[0] \r\n"; 
}

现在您可以使用其他数据了:

Now you can work with other data:

foreach ($labels as $ind=>$item){

        $index = $ind + 2;

    foreach($item as $ind2=>$data){

        $letter = range('A', 'Z')[$ind2];

        echo "$letter$index \r\n";

        $val = explode('>',$data);

        //$objPHPExcel->getActiveSheet()->setCellValue("$letter$index",$val[1]);
        echo "Value at $index -> $val[1] \r\n\r\n";
    }
    echo "\r\n\r\n";
}

演示

Demo

注意:此代码对A..Z范围列索引适用,对于其他代码,您需要更新此代码.

这篇关于如何在phpExcel中将数组打印为行和列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆