Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I'm working with php_xlsxwriter, which takes php and outputs it to Excel.

Their sample code for "what gets output" is:

$data2 = array(
    array('2003','01','343.12'),
    array('2003','02','353.12'),
    array('2003','03','363.12'),
    array('2003','04','373.12'),
);

which of course outputs 4 lines to an Excel spreadsheet. For some reason ONLY an array written like this will output successfully. I've tried building arrays in other ways, but it causes the plugin to fail.

As such, I'm inputting data from an ajax call into this page, which contains my array of say... 50 elements, but I can't figure out how to write the foreach loop so that it builds a complete list. Right now, it's just outputting the LAST element in the array.

foreach ($xlString as &$c) {

  $s = $c[0];
  $p = $c[1];
  $l = $c[2];
  $a = $c[3]; 

  $data1 = array(

      array($s,$p,$l,$a),

  );

}

I understand WHY this is - because I'm overwriting $data1 each time I run the foreach loop - but I don't know how to change the code so that I don't do this.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

Declare $data outside the array, then add your results as a new element within the loop

$data = array()
foreach ($xlString as &$c) {

  $s = $c[0];
  $p = $c[1];
  $l = $c[2];
  $a = $c[3]; 

  $data1[] = array(

      array($s,$p,$l,$a),

  );

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.