How can i print array as row and column in phpExcel?

假装没事ソ 提交于 2020-03-28 06:39:09

问题


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
) 

code -

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++;
                }

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; 

回答1:


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

Note: this code is OK for A..Z range column indexes, for others you need to update this code.



来源:https://stackoverflow.com/questions/59945956/how-can-i-print-array-as-row-and-column-in-phpexcel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!