Jump to content

PHP: My object isn't being treated as an array when is_array says it is one.

Go to solution Solved by Cruorzy,

If it gives an array you should give the values of the array to the csv with a foreach statement.

I never worked with the fputcsv function but according to w3schools it should be something like this.

 

$file = fopen("contacts.csv","w");

foreach ($data_array as $line)
{
	fputcsv($file, $line);
}

 

So, I have an array being given to me by a MySQL query. Here is the array, as it is printed by print_r function:

Array ( [policydatakey] => 1 [policydatapolicykey] => 1 [policydatastatus] => Active [policydatapolicyname] => Policy 1111 [policydataprocessorname] => My Processor [policydatabranchcode] => Branch1 [policydatafiletaxid] => Taxid1 [policydatafilestate] => XX [policydatafilelicense] => Lic111 [policydatainsuredname] => Trim [policydatarisklocation] => 4 [policydataunderwritingcompany] => Stuff n Thangs [policydatapolicystate] => XX [policydatapolicynumber] => 1111 [policydatapolicytype] => 8 [policydatamultistate] => 9 [policydatalineofbusiness] => 0 [policydataeffectivedate] => 1 [policydataendorsementeffectivedate] => 2 [policydataenddate] => 3 [policydatapremium] => 4 [policydatainsuredamount] => 5 [policydatapolicyfee] => 6 [policydatainspectionfee] => 7 [policydatacatfee] => 8 [policydatasltaxes] => 9 [policydatafiremarshaltax] => 0 [policydatasurcharge] => 5-surcharge [policydataadditionalassessment] => 2 [policydatastampingfee] => 3 [policydatainvoicenumber] => XXXX [policydatainvoicedate] => 2016-02-02 [policydataac1] => 6 [policydataac1date] => 2016-01-01 [policydataac1comments] => 8 [policydataac2] => 9 [policydataac2date] => 0 [policydataac2comments] => 1 [policydataac3] => yes (ac3) [policydataac3date] => 3 [policydataac3comments] => Ac3 Comments go here [policydatacomments] => My Comments )

When I run is_array on the variable that holds that array, it returns boolean TRUE. i.e.

if (is_array($data)==TRUE){
	printf("It is an array.");
}else{
	printf("It is not an array.");
}

Comes back as "It is an array."

When I try to have that array printed to a .CSV using fputcsv(), I get the following:

Catchable fatal error: Argument 2 passed to fputcsv() must be an array, null given...

When I try to have that array printed to a spreadsheet XLSX file, using xlsxwriter.class.php (see here), it complains of the same thing.

Neither have a problem using a simple test array, as follows:

$array = array(
	array('year','month','day'),
	array('2004','8','20'),
	array('2008','12','13'),
	);

The way I'm getting the array I'm trying to use is with the following:

$data = $conn->query($sql); // $conn is the connection credentials to the MySQL Database, and $sql is the sql query.
$data_array = array();
$data_array = $data->fetch_assoc();

fetch_assoc() is supposed to return an associative array, which it seems to, rather than an object.

I have no idea what the issue is.

† Christian Member †

For my pertinent links to guides, reviews, and anything similar, go here, and look under the spoiler labeled such. A brief history of Unix and it's relation to OS X by Builder.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

If it gives an array you should give the values of the array to the csv with a foreach statement.

I never worked with the fputcsv function but according to w3schools it should be something like this.

 

$file = fopen("contacts.csv","w");

foreach ($data_array as $line)
{
	fputcsv($file, $line);
}

 

Quote or mention me if not feel ignored 

Link to comment
Share on other sites

Link to post
Share on other sites

22 hours ago, Cruorzy said:

If it gives an array you should give the values of the array to the csv with a foreach statement.

I never worked with the fputcsv function but according to w3schools it should be something like this.


$file = fopen("contacts.csv","w");

foreach ($data_array as $line)
{
	fputcsv($file, $line);
}

 

So, this is what it returns when I try that:

Warning: fputcsv() expects parameter 2 to be array, string given in /home/comp/public_html/db/db_PullData_ExportToXLS.php on line 80

And it repeats that for every line of the Array, saying it's a string.

If I try just doing:

fputcsv($file,$data_array);

That actually works.... welp, IDK why it didn't the first time.

However, XLSX isn't treating it as an array still. Unfortunate, but I can make a CSV work. Thanks :D.

† Christian Member †

For my pertinent links to guides, reviews, and anything similar, go here, and look under the spoiler labeled such. A brief history of Unix and it's relation to OS X by Builder.

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×