1

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.

1 Answer 1

1

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),

  );

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.