Exporting array to PHPExcel producing array to string conversion error

Active3 hr before
Viewed126 times

9 Answers


Thanks for contributing an answer to Stack Overflow!,Asking for help, clarification, or responding to other answers., Stack Overflow for Teams Where developers & technologists share private knowledge with coworkers ,Connect and share knowledge within a single location that is structured and easy to search.

You'll need to cast the objects to arrays first, to make it a 2d-array:

   function( & $row) {
      $row = (array) $row;
$this - > excel - > getActiveSheet() - > fromArray($ordersArray, NULL, 'A1');

$gadget = array('computer', 'mobile', 'tablet');
echo implode($arr);
load more v

Parses a string input for fields in CSV format and returns an array containing the fields read. , Returns an indexed array containing the fields read. ,str_getcsv — Parse a CSV string into an array ,fgetcsv() - Gets line from file pointer and parse for CSV fields

load more v

To change the default behaviour, you can use excel::export.sheets.strictNullComparison config setting.,By default 0 is shown as an empty cell. If you want to change this behaviour, you can pass true as 4th parameter:,By default the export will use the keys of your array (or model attribute names) as first row (header column). To change this behaviour you can edit the default config setting (excel::export.generate_heading_by_indices) or pass false as 5th parameter:,Creating a sheet from an array

Excel::create('Filename', function($excel) {

      $excel - > sheet('Sheetname', function($sheet) {

         $sheet - > fromArray(array(
            array('data1', 'data2'),
            array('data3', 'data4')


   }) - >
   export ('xls');
load more v

If you have a simple 1-d array, and want to write it as a column, then the following will convert it into an appropriately structured 2-d array that can be fed to the fromArray() method:,Formats handled by the advanced value binder include:,You can read more about value binders later in this section of the documentation.,By default, PhpSpreadsheet will automatically detect the value type and set it to the appropriate Excel numeric datatype. This type conversion is handled by a value binder, as described in the section of this document entitled "Using value binders to facilitate data entry".

Setting a cell value by coordinate can be done using the worksheet's setCellValue() method.

// Set cell A1 with a string value
$spreadsheet - > getActiveSheet() - > setCellValue('A1', 'PhpSpreadsheet');

// Set cell A2 with a numeric value
$spreadsheet - > getActiveSheet() - > setCellValue('A2', 12345.6789);

// Set cell A3 with a boolean value
$spreadsheet - > getActiveSheet() - > setCellValue('A3', TRUE);

// Set cell A4 with a formula
$spreadsheet - > getActiveSheet() - > setCellValue(
   '=IF(A3, CONCATENATE(A1, " ", A2), CONCATENATE(A2, " ", A1))'
load more v

I’m not sure what I’m doing incorrectly here but I keep getting "Array to string conversion" error.,I’m using a livewire form to dynamically add more rows if a user needs to add more "attachments" which are just separate strings.,I believed I was taking an array from the blade, validating it and then imploding it into a string for storage but obviously I’m missing something., Error while posting form in laravel (CORS)

Relevant parts to my Livewire "controller":

public $attachments = [];

function mount($traffic) {
   $this - > attachments = [

function save() {
   $data = $this - > validate([
      'attachments' => 'nullable',

   $traffic = Traffic::create([
      'attachments' => implode(', ', $data['attachments']),
load more v

This is a short PHP guide on how to fix the “Array to string conversion” error. This is a common notice that appears whenever you attempt to treat an array like a string.,The main thing to understand here is that you cannot treat an array like a string. If you attempt to do so, PHP will display a notice.,To solve this particular error, we can add a simple check before attempting to output each element:,Notice: Array to string conversion in C:\wamp\www\test\index.php on line 7

To reproduce this error, you can run the following code:

//Simple PHP array.
$array = array(1, 2, 3);

//Attempt to print the array.
echo $array;
load more v

The following examples use the dataset created for and earlier article Sorting Arrays of Arrays which is defined as follows:,Text or number converted to unintended number format in Excel,Further down this page you will find examples for creating a downloadable file using data from an SQL query. ,This is the entire script required to query the database, clean the data, and trigger a file download.

for me to make utf-8 work, you have to put utf-8 BOM like this $out = fopen("php://output", 'w+'); fwrite($out, "\xEF\xBB\xBF");

$out = fopen("php://output", 'w+');
fwrite($out, "\xEF\xBB\xBF");
load more v

We can also perform this export action via programming. In PHP, the code implementation of creating a database export tool is simple.,In this example, we are going to create PHP code for taking backup by exporting a database.,We have already seen several example code for implementing database export using PHP.,In PHP, it handles the form submit to process export. First, we retrieve the product results by accessing the database. Then, then we pass the resultant product array as an argument to the export function.

This database script is added to the downloadable source code as schema.sql.

--Table structure
for table `tbl_products`

CREATE TABLE `tbl_products`(
   int(8) NOT NULL,
   varchar(255) NOT NULL,
   double(10, 2) NOT NULL,
   varchar(255) NOT NULL,
   text NOT NULL,
   float(3, 1) NOT NULL

--Dumping data
for table `tbl_products`

INSERT INTO `tbl_products`(`id`, `name`, `price`, `category`, `product_image`, `average_rating`) VALUES(1, 'Tiny Handbags', 100.00, 'Fashion', 'gallery/handbag.jpeg', 5.0),
   (2, 'Men\'s Watch', 300.00, 'Generic', 'gallery/watch.jpeg', 4.0),
   (3, 'Trendy Watch', 550.00, 'Generic', 'gallery/trendy-watch.jpeg', 4.0),
   (4, 'Travel Bag', 820.00, 'Travel', 'gallery/travel-bag.jpeg', 5.0),
   (5, 'Plastic Ducklings', 200.00, 'Toys', 'gallery/ducklings.jpeg', 4.0),
   (6, 'Wooden Dolls', 290.00, 'Toys', 'gallery/wooden-dolls.jpeg', 5.0),
   (7, 'Advanced Camera', 600.00, 'Gadget', 'gallery/camera.jpeg', 4.0),
   (8, 'Jewel Box', 180.00, 'Fashion', 'gallery/jewel-box.jpeg', 5.0),
   (9, 'Perl Jewellery', 940.00, 'Fashion', 'gallery/perls.jpeg', 5.0);

for table `tbl_products`
ALTER TABLE `tbl_products`

ALTER TABLE `tbl_products`
load more v

Other "string-array" queries related to "Exporting array to PHPExcel producing array to string conversion error"