-1

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; 
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98
commlad_
  • 1
  • 1
  • Please go read [ask]. It is currently rather unclear, what actual result you want to achieve here, so please add an example of that, matching your input data. And the code you have shown so far does not seem to have anything in particular to do with phpexecl or phpspreadsheet. – 04FS Jan 28 '20 at 09:48
  • What value should be a row, what a column? For example explain the first index [0]. – Aksen P Jan 28 '20 at 09:53
  • Show what you actually tried then. Ask a clear question that explains what _specific_ problem you are having. – 04FS Jan 28 '20 at 09:54
  • reference:- https://stackoverflow.com/questions/27784737/phpexcel-add-column-dynamically – Alive to die - Anant Jan 28 '20 at 09:55
  • https://stackoverflow.com/questions/2584954/phpexcel-how-to-set-cell-value-dynamically – Alive to die - Anant Jan 28 '20 at 10:12
  • Thanks @AnantSingh---AlivetoDie but at `$objPHPExcel->getActiveSheet() ->setCellValue($exploded[0],$counter,$exploded[1]);` it says `undefine offsets` – commlad_ Jan 28 '20 at 10:26
  • @AksenP Value of Column should be `[0] => EY` and row should be `Yes` – commlad_ Jan 28 '20 at 10:31
  • @AnantSingh---AlivetoDie $name holds the array data – commlad_ Jan 28 '20 at 11:03

1 Answers1

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.

Aksen P
  • 4,564
  • 3
  • 14
  • 27