I have an array of 10 objects that I am trying to convert to JSON. The array is being stored in a variable called $invoices
. Below is the ouput of var_dump($invoices)
shorted to only the first object.
I've tried the following:
$invoices = json_encode($invoices, FALSE);
$invoices = json_encode($invoices, TRUE);
$invoices = json_encode($invoices, JSON_UNESCAPED_UNICODE);
However, the output is always:
string(31) "[{},{},{},{},{},{},{},{},{},{}]"
How can this array be properly converted to JSON?
Note: I am running PHP 5.5.9
Update:
It turns out the problem is due the fact that json_encode won't work with "protected member variables." Is there a way to declare those variables as public if I don't have access to the class that created them?
array(10) {
[0]=>
object(QuickBooks_IPP_Object_Invoice)#285 (1) {
["_data":protected]=>
array(22) {
["Id"]=>
array(1) {
[0]=>
string(6) "{-224}"
}
["SyncToken"]=>
array(1) {
[0]=>
string(1) "0"
}
["MetaData"]=>
array(1) {
[0]=>
object(QuickBooks_IPP_Object_MetaData)#282 (1) {
["_data":protected]=>
array(2) {
["CreateTime"]=>
array(1) {
[0]=>
string(25) "2014-12-07T09:48:47-08:00"
}
["LastUpdatedTime"]=>
array(1) {
[0]=>
string(25) "2014-12-07T09:48:47-08:00"
}
}
}
}
["CustomField"]=>
array(1) {
[0]=>
object(QuickBooks_IPP_Object_CustomField)#292 (1) {
["_data":protected]=>
array(3) {
["DefinitionId"]=>
array(1) {
[0]=>
string(4) "{-1}"
}
["Name"]=>
array(1) {
[0]=>
string(6) "Crew #"
}
["Type"]=>
array(1) {
[0]=>
string(10) "StringType"
}
}
}
}
["DocNumber"]=>
array(1) {
[0]=>
string(4) "1038"
}
["TxnDate"]=>
array(1) {
[0]=>
string(10) "2014-12-07"
}
["Line"]=>
array(2) {
[0]=>
object(QuickBooks_IPP_Object_Line)#263 (1) {
["_data":protected]=>
array(5) {
["Id"]=>
array(1) {
[0]=>
string(4) "{-1}"
}
["LineNum"]=>
array(1) {
[0]=>
string(1) "1"
}
["Amount"]=>
array(1) {
[0]=>
string(9) "155555.00"
}
["DetailType"]=>
array(1) {
[0]=>
string(19) "SalesItemLineDetail"
}
["SalesItemLineDetail"]=>
array(1) {
[0]=>
object(QuickBooks_IPP_Object_SalesItemLineDetail)#765 (1) {
["_data":protected]=>
array(2) {
["ItemRef"]=>
array(1) {
[0]=>
string(4) "{-3}"
}
["TaxCodeRef"]=>
array(1) {
[0]=>
string(6) "{-NON}"
}
}
}
}
}
}
[1]=>
object(QuickBooks_IPP_Object_Line)#748 (1) {
["_data":protected]=>
array(3) {
["Amount"]=>
array(1) {
[0]=>
string(9) "155555.00"
}
["DetailType"]=>
array(1) {
[0]=>
string(18) "SubTotalLineDetail"
}
["SubTotalLineDetail"]=>
array(1) {
[0]=>
string(0) ""
}
}
}
}
["TxnTaxDetail"]=>
array(1) {
[0]=>
object(QuickBooks_IPP_Object_TxnTaxDetail)#287 (1) {
["_data":protected]=>
array(1) {
["TotalTax"]=>
array(1) {
[0]=>
string(1) "0"
}
}
}
}
["CustomerRef"]=>
array(1) {
[0]=>
string(5) "{-11}"
}
["BillAddr"]=>
array(1) {
[0]=>
object(QuickBooks_IPP_Object_BillAddr)#284 (1) {
["_data":protected]=>
array(7) {
["Id"]=>
array(1) {
[0]=>
string(5) "{-11}"
}
["Line1"]=>
array(1) {
[0]=>
string(13) "1045 Main St."
}
["City"]=>
array(1) {
[0]=>
string(13) "Half Moon Bay"
}
["CountrySubDivisionCode"]=>
array(1) {
[0]=>
string(2) "CA"
}
["PostalCode"]=>
array(1) {
[0]=>
string(5) "94213"
}
["Lat"]=>
array(1) {
[0]=>
string(10) "37.4559621"
}
["Long"]=>
array(1) {
[0]=>
string(11) "-122.429939"
}
}
}
}
["ShipAddr"]=>
array(1) {
[0]=>
object(QuickBooks_IPP_Object_ShipAddr)#814 (1) {
["_data":protected]=>
array(7) {
["Id"]=>
array(1) {
[0]=>
string(5) "{-11}"
}
["Line1"]=>
array(1) {
[0]=>
string(13) "1045 Main St."
}
["City"]=>
array(1) {
[0]=>
string(13) "Half Moon Bay"
}
["CountrySubDivisionCode"]=>
array(1) {
[0]=>
string(2) "CA"
}
["PostalCode"]=>
array(1) {
[0]=>
string(5) "94213"
}
["Lat"]=>
array(1) {
[0]=>
string(10) "37.4559621"
}
["Long"]=>
array(1) {
[0]=>
string(11) "-122.429939"
}
}
}
}
["DueDate"]=>
array(1) {
[0]=>
string(10) "2015-01-06"
}
["TotalAmt"]=>
array(1) {
[0]=>
string(9) "155555.00"
}
["ApplyTaxAfterDiscount"]=>
array(1) {
[0]=>
string(5) "false"
}
["PrintStatus"]=>
array(1) {
[0]=>
string(11) "NeedToPrint"
}
["EmailStatus"]=>
array(1) {
[0]=>
string(6) "NotSet"
}
["Balance"]=>
array(1) {
[0]=>
string(9) "155555.00"
}
["Deposit"]=>
array(1) {
[0]=>
string(1) "0"
}
["AllowIPNPayment"]=>
array(1) {
[0]=>
string(5) "false"
}
["AllowOnlinePayment"]=>
array(1) {
[0]=>
string(5) "false"
}
["AllowOnlineCreditCardPayment"]=>
array(1) {
[0]=>
string(5) "false"
}
["AllowOnlineACHPayment"]=>
array(1) {
[0]=>
string(5) "false"
}
}
}
[1]=>
object(QuickBooks_IPP_Object_Invoice)#830 (1) {
["_data":protected]=>
array(22) {
...
...